Skip to content

fix(ui): provide the ssr browser context above ClientContext - #10634

Merged
GiladShoham merged 4 commits into
masterfrom
fix/ssr-browser-context-coverage
Aug 19, 2026
Merged

GiladShoham merged 4 commits into
masterfrom
fix/ssr-browser-context-coverage

Conversation

@GiladShoham

Copy link
Copy Markdown
Member

useUserAgent reads ssrBrowserContext, and that context is provided in exactly one place — wrapping only {hudItems} and {routes}, inside ClientContext. Everything rendered outside that subtree therefore gets undefined, falls back to window.navigator, and throws ReferenceError: window is not defined on the server. The ssr middleware swallows it and serves the empty client shell, so the page still looks fine in a browser.

ClientContext is not a bare provider: it renders icons, the theme switcher, the loader ribbon and the tooltip mount point. Any of those growing a useIsMobile call — directly or through a dependency bump — takes scope SSR down. Swapping the nesting so SSRBrowserProvider wraps ClientContext closes the whole class.

Why this surfaced now

Reported from a branch that removes core envs from the manifest, where the crash reproduces deterministically:

[ssr] failed at '/'
ReferenceError: window is not defined
    at useUserAgent → useIsMobile → Tooltip

That branch already has all of #10628's fixes, so this is not a regression of them — it is a second, independent gap that #10628 did not cover. It was latent on master only because nothing inside ClientContext currently happens to call useIsMobile; changing which aspects load changes what renders there.

Worth being explicit that the .cjs fix and the use-user-agent alias from #10628 are still doing their jobs. This is a tree-position bug, not a duplicate-context one — the alias pins the module to a single copy, so provider and consumer share the context object; the consumer is simply rendered where no provider is above it.

Proof

Rendering a Tooltip inside ClientContext — what the reporting branch effectively does — and changing nothing else:

nesting ui-ssr.e2e.ts
ClientContext > SSRBrowserProvider (before) 2 failing — #root empty, ssr fell back to the client shell
SSRBrowserProvider > ClientContext (after) 4 passing

The synthetic Tooltip was then removed; both UI suites pass on the final diff (16 assertions).

Residual gap

Render-plugin reactContexts (pubsub, lanes, notifications, user-agent) are applied by ServerRenderer outside the JSX this file passes, so they remain above the provider. A component rendered by one of those contexts — as opposed to inside its children — would still hit this. Covering that needs ssrBrowserContext supplied as a render plugin rather than as JSX, which is a larger change and not what the reported failure needs.

Also worth noting separately: scopes/ui-foundation/user-agent provides userAgentContext, which useUserAgent never reads — it only reads ssrBrowserContext. That aspect looks vestigial for this purpose.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Wrap ClientContext with the SSR browser provider

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Moves SSR browser context above ClientContext in the server-rendered React tree.
• Prevents nested UI components from accessing window.navigator during SSR.
Diagram

graph TD
  A["Server Renderer"] --> B["SSR Browser Provider"] --> C["Client Context"] --> D["HUD Elements"]
  C --> E["Application Routes"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Register browser context as an SSR render plugin
  • ➕ Would also cover components rendered by outer render-plugin contexts.
  • ➕ Would make browser context part of the renderer-wide provider stack.
  • ➖ Requires a broader change to pass session-specific browser data into the plugin.
  • ➖ Increases implementation and regression risk beyond the reported failure.

Recommendation: Keep this targeted provider-order fix because it safely covers ClientContext, HUD elements, and routes with minimal risk. Consider render-plugin integration separately if components owned by outer plugin contexts also require SSR browser metadata.

Files changed (1) +12 / -4

Bug fix (1) +12 / -4
ui.ui.runtime.tsxPlace SSR browser context above ClientContext +12/-4

Place SSR browser context above ClientContext

• Reverses the server-rendered provider nesting so ClientContext and all of its descendants receive SSR browser metadata. This prevents user-agent hooks from falling back to window.navigator during server rendering.

scopes/ui-foundation/ui/ui.ui.runtime.tsx

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can show, collapse, or hide each part of a finding: code, evidence, and all

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 3022b2a ⚖️ Balanced

Results up to commit c17bbe4


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review
Results up to commit 3659f7c


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review
Results up to commit 5a2cf77


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@GiladShoham
GiladShoham enabled auto-merge (squash) August 19, 2026 11:44
@github-actions github-actions Bot mentioned this pull request Aug 19, 2026
GiladShoham added a commit that referenced this pull request Aug 19, 2026
…nst it

setup_esbuild_bundle now runs `bit build ... --tasks BundleUI,PreBundlePreview`
and saves the result to .bundle-cache/ before bundling, so the esbuild bundle
ships with a real pre-bundle instead of none at all. e2e_test_esbuild_bundle
sets BIT_E2E_UI_MODE=prebuilt so ui-start.e2e.ts/ui-ssr.e2e.ts run for real as
part of its normal parallel sweep instead of skipping.

Also corrects the known-gaps/findings-log entries: cherry-picked #10634 (the
ssr-browser-context fix opened off this branch's SSR-crash finding) does not
close that gap - re-tested and the identical crash still reproduces, since our
failure goes through a render-plugin context #10634's own description already
calls out as not covered. Kept the cherry-pick since it's a correct, verified
fix for what it does target.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code review by qodo was updated up to the latest commit 3659f7c

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code review by qodo was updated up to the latest commit c17bbe4

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code review by qodo was updated up to the latest commit 3022b2a

@GiladShoham
GiladShoham merged commit 73657ac into master Aug 19, 2026
14 checks passed
@GiladShoham
GiladShoham deleted the fix/ssr-browser-context-coverage branch August 19, 2026 15:12
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