Skip to content

experiment(react-web-sdk): stub SDK on server#349

Closed
David Nalchevanidze (nalchevanidze) wants to merge 1 commit into
mainfrom
experiment/ssr-stub
Closed

experiment(react-web-sdk): stub SDK on server#349
David Nalchevanidze (nalchevanidze) wants to merge 1 commit into
mainfrom
experiment/ssr-stub

Conversation

@nalchevanidze

Copy link
Copy Markdown
Contributor

Approach

Adds an in-process SSR stub that satisfies instanceof ContentfulOptimization and is returned by useOptimization() during the window before useLayoutEffect fires (SSR + first client render). Components render without throwing; all real SDK calls happen in effects or event handlers which run after the SDK is already live.

Changes

  • ssrStub.ts — full no-op OptimizationSdk shape with Object.setPrototypeOf(stub, ContentfulOptimization.prototype)
  • useOptimization.ts — return SSR_STUB instead of throwing when SDK is not yet ready
  • OptimizationProvider.tsx — always render children unless onStatesReady is set and SDK is not yet ready (allows Next.js SSR to produce HTML from server components inside the provider tree)
  • Test updates to match new render behaviour (renderedChild: true during SSR, liveUpdates renders [true, false, true, false, true, true])

Trade-offs vs the other experiments

This PR (stub) #346 (undefined) #347 (ssr-sdk-init)
Call sites No changes needed Guard at every hook call site No changes needed
Bundle size +stub object No change +LocalStore guard
Server SDK init No No Yes
SSR_STUB ownership React SDK React SDK Not needed

🤖 Generated with Claude Code

…e SDK is ready

Adds ssrStub.ts with a full no-op OptimizationSdk shape that satisfies
instanceof ContentfulOptimization checks. useOptimization returns it
during the first render window (SSR + client pre-useLayoutEffect) so
components can render without throwing. OptimizationProvider always
renders children unless onStatesReady is set and the SDK is not yet ready.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
David Nalchevanidze (nalchevanidze) added a commit that referenced this pull request Jul 1, 2026
…t/ssr-stub

The stub approach is now isolated in PR #349. fix-ssr contains only
the nextjs-sdk implementation and surrounding changes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nalchevanidze David Nalchevanidze (nalchevanidze) changed the title experiment(react-web-sdk): Return SSR stub from useOptimization before SDK is ready experiment(react-web-sdk): stub SDK Jul 1, 2026
@nalchevanidze David Nalchevanidze (nalchevanidze) changed the title experiment(react-web-sdk): stub SDK experiment(react-web-sdk): stub SDK on server Jul 1, 2026
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.

1 participant