Skip to content

Implement global context and use agnostic solution#260

Open
TRomesh wants to merge 12 commits intomainfrom
feature/CMS-50430-implement-global-context
Open

Implement global context and use agnostic solution#260
TRomesh wants to merge 12 commits intomainfrom
feature/CMS-50430-implement-global-context

Conversation

@TRomesh
Copy link
Copy Markdown
Member

@TRomesh TRomesh commented Mar 20, 2026

  • Used adapter pattern that works with any framework. React version uses React.cache() to keep data separate for each request.

  • Added methods getContext() / setContext(data) to work with all data at once; getContextData(key) / setContextData(key, value) to get/set one piece at a time.

  • Save data like preview_token, locale, version, type, currentContent. Automatically filled when fetching preview content. Each request gets its own private copy.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a framework-agnostic, adapter-based “global context” system intended to provide request-scoped data (e.g., preview_token, locale, version, type, etc.) across the React Server Component tree, and updates the Next.js template + docs to wrap pages with a new withAppContext HOC.

Changes:

  • Added a new context subsystem (adapter interface + configuration + React adapter) and exposed it via @optimizely/cms-sdk/react/server.
  • Auto-populated context from GraphClient.getPreviewContent() and used context in RichText image rendering to append preview tokens.
  • Updated the alloy Next.js template and docs to use withAppContext.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
templates/alloy-template/src/app/preview/page.tsx Wraps preview page export with withAppContext.
templates/alloy-template/src/app/[...slug]/page.tsx Wraps slug page export with withAppContext.
packages/optimizely-cms-sdk/src/react/server.tsx Re-exports context APIs + withAppContext from the React server entrypoint.
packages/optimizely-cms-sdk/src/react/richText/renderer.ts Formatting/trailing comma updates.
packages/optimizely-cms-sdk/src/react/richText/lib.ts Appends preview token to image URLs using context.
packages/optimizely-cms-sdk/src/react/context/contextWrapper.tsx Adds withAppContext HOC and configures the React adapter.
packages/optimizely-cms-sdk/src/react/context/test/contextWrapper.test.tsx Tests for withAppContext.
packages/optimizely-cms-sdk/src/graph/index.ts Auto-populates context during getPreviewContent().
packages/optimizely-cms-sdk/src/graph/test/index.test.ts Removes old removeTypePrefix test file (moved/expanded elsewhere).
packages/optimizely-cms-sdk/src/graph/test/context.test.ts Adds tests for context auto-population + existing removeTypePrefix tests.
packages/optimizely-cms-sdk/src/context/reactContextAdapter.ts Adds React adapter based on React.cache().
packages/optimizely-cms-sdk/src/context/config.ts Adds adapter configuration + getContext/setContext/getContextData/setContextData.
packages/optimizely-cms-sdk/src/context/baseContext.ts Defines ContextData and ContextAdapter contract.
packages/optimizely-cms-sdk/src/context/test/reactContextAdapter.test.ts Adds adapter contract/documentation tests.
packages/optimizely-cms-sdk/src/context/test/config.test.ts Adds config API tests.
docs/7-live-preview.md Documents withAppContext and context access patterns.
docs/6-rendering-react.md Documents withAppContext and context access patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

… adapter configuration; improve error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants