Implement global context and use agnostic solution#260
Open
Implement global context and use agnostic solution#260
Conversation
…est-scoped storage
…ths for React integration
…st-scoped context integration
…est coverage for GraphClient integration
…date error handling for broken adapters
…on in getPreviewContent
…r and ReactContextAdapter; implement tests for context integration
…; update related tests and components accordingly
Contributor
There was a problem hiding this comment.
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.
packages/optimizely-cms-sdk/src/react/context/contextWrapper.tsx
Outdated
Show resolved
Hide resolved
…ror handling for preview token retrieval
Contributor
There was a problem hiding this comment.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.