experiment(react-web-sdk): undefined SDK on server#346
Draft
David Nalchevanidze (nalchevanidze) wants to merge 3 commits into
Draft
experiment(react-web-sdk): undefined SDK on server#346David Nalchevanidze (nalchevanidze) wants to merge 3 commits into
David Nalchevanidze (nalchevanidze) wants to merge 3 commits into
Conversation
Demonstrates the cost of removing the stub pattern. All hooks that consumed useOptimization() now require explicit sdk guards at every call site, and callers that received the sdk directly (dev/App.tsx, onStatesReady tests) must handle undefined — surfacing the full blast radius of this change. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Dev app uses early-return guard (if (!sdk) return null) — the pattern every consumer component would need to adopt. Test probes use optional chaining (?.states) to handle the undefined path. These fixups show the propagation cost at concrete call sites. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Shows how the SSR implementation's direct sdk usage sites must also be updated: effect-based calls need if (!sdk) guards, event handler calls need optional chaining. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This was referenced Jul 1, 2026
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.
What this shows
This branch demonstrates what it would cost to return `undefined` from `useOptimization()` instead of the current `SSR_STUB` approach. It builds on top of `fix-ssr`, so all the SSR implementation work is included — you can see how the undefined path interacts with the actual SSR rendering.
Changes
`useOptimization.ts`
Returns `OptimizationSdk | undefined` — `undefined` before the SDK is ready (SSR and first client render).
All dependent hooks (7 files)
Each hook that consumed `useOptimization()` now needs explicit null guards:
Callers: package layer
Callers: nextjs-sdk_ssr implementation
Verdict
The undefined path distributes the SSR_STUB cost outward: instead of one central stub that makes the SDK always non-null, every hook and every direct consumer of `useOptimization()` must handle `undefined` explicitly. The API surface (`UseOptimizationActionsResult`) widens, and components that receive the SDK as a prop would also need to accept `undefined`.
The SSR_STUB approach keeps the contract simple: hooks always get a callable SDK, and the stub's no-ops are safe because real SDK calls happen in effects (after `useLayoutEffect` has set the real SDK). This branch shows the alternative is real but costly — every new direct caller of `useOptimization()` would need to know about and handle the undefined window.