Skip to content

[tabs] Position pre-hydration indicator inside streamed Suspense - #5171

Merged
brijeshb42 merged 11 commits into
mui:masterfrom
brijeshb42:tabs-indicator-prehydration-streaming
Jul 16, 2026
Merged

brijeshb42 merged 11 commits into
mui:masterfrom
brijeshb42:tabs-indicator-prehydration-streaming

Conversation

@brijeshb42

@brijeshb42 brijeshb42 commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

With streaming SSR, <Tabs.Indicator renderBeforeHydration /> stays [hidden] until hydration when the tabs are below a Suspense boundary that resolves after the shell. React parses such content inside a hidden container (<div hidden id="S:…">), so everything measures 0×0 and the prehydration script bails permanently on its zero-size guard.

Repro: https://github.com/brijeshb42/base-ui-tabs-indicator-streaming-repro

There's also an experiment page in this PR (/experiments/tabs/tabs-streaming-ssr, needs pnpm docs:dev since it relies on server rendering) with the same setup against the repo source: a control tab bar, a streamed one, and hydration held back 5s so the pre-hydration window is easy to observe. Can be removed before merge.

Screen.Recording.2026-07-04.at.10.52.44.PM.mov

Fix

On zero size, observe the tabs list with a ResizeObserver: inside the hidden segment it has no box so nothing fires, and when React reveals the segment it gains one — the callback runs after layout but before paint, so the indicator still never flashes in late. It disconnects on success or when hydration gets there first. The measurable-at-parse-time path is unchanged, and browsers without ResizeObserver degrade to today's behavior (wait for hydration).

Verified with the repro in headless Chromium (hydration delayed 4s): the streamed indicator goes from {hidden: true, width: 0} to {hidden: false, width: 62} pre-hydration.

The slider thumb prehydration script has the same failure mode; left out to keep this focused. Can be updated to have same behaviour.

@pkg-pr-new

pkg-pr-new Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5171
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5171
    

commit: 802594e

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

Total duration: 1,158.24 ms -50.52 ms(-4.2%) | Renders: 78 (+0) | Paint: 1,785.66 ms -95.85 ms(-5.1%)

Test Duration Renders
Checkbox mount (500 instances) 77.36 ms 🔺+17.04 ms(+28.2%) 1 (+0)

13 tests within noise — details

Metric alarms

Test Metric Change
Checkbox mount (500 instances) bench:paint 🔺 +21.35 ms

Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jul 4, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 802594e
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a5885df3377a6000893cd73
😎 Deploy Preview https://deploy-preview-5171--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@brijeshb42 brijeshb42 added component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected. labels Jul 4, 2026
@brijeshb42
brijeshb42 requested a review from aarongarciah as a code owner July 4, 2026 17:31
@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch from e6257e8 to f711d6e Compare July 4, 2026 17:41
@brijeshb42 brijeshb42 changed the title [tabs] Position pre-hydration indicator inside streamed Suspense boundaries [tabs] Position pre-hydration indicator inside streamed Suspense Jul 4, 2026
@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch 2 times, most recently from 5bed197 to a1a38a3 Compare July 8, 2026 11:03
@brijeshb42
brijeshb42 requested a review from a team July 8, 2026 11:03

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review summary

The core rewrite looks correct. I regenerated the minified script from the template with terser (ecma: 2020) and it matches the committed prehydrationScript.min.ts byte-for-byte, and the common (non-streaming) SSR path is behaviorally identical to before (the removed if (activeTab != null && tabsList != null) block was already dead — both were guaranteed non-null by the earlier early-returns). The diff is a net simplification on top of the fix.

One non-blocking finding left inline: a newly-introduced ResizeObserver/DOM retention on an abandoned-before-reveal lifecycle.

Note — test coverage: the new ResizeObserver path has no automated coverage; the existing pre-hydration rendering tests only assert the script's presence, nonce, and post-hydration removal, not its runtime effect. That's hard to address in the current harness (jsdom has no layout so offsetWidth is always 0 and inline scripts aren't executed; the chromium env resolves #prehydration/tabs/indicator to the empty stub), so relying on the headless-Chromium repro + the experiment page is a reasonable call — flagging so the gap is a deliberate choice.

Verdict: Approve after nits — no correctness regressions; the only actionable item is the narrow-window observer retention, which can be mitigated or knowingly accepted.


🤖 Review generated with Claude Code

@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch from a1a38a3 to f3009e9 Compare July 8, 2026 12:32
@brijeshb42
brijeshb42 requested a review from atomiks July 8, 2026 12:33
@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch 4 times, most recently from 9e126c7 to 388f08a Compare July 13, 2026 14:56
@brijeshb42

Copy link
Copy Markdown
Contributor Author

@atomiks Updated to add a lifetime timer which cancels the observer after 10sec of inactivity.

@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch from 388f08a to 622de68 Compare July 14, 2026 04:30
@atomiks

atomiks commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

PR review

Two correctness issues remain, one merge-blocking. I reproduced both DOM-level mechanisms in headless Chromium using the PR’s observer logic. I could not run the repository test suite locally.

Bugs (2)

1. 🔴 Hydration can resurrect an indicator for a stale server tab

Location: packages/react/src/tabs/indicator/prehydrationScript.template.js:95

const activeTab = tabsList.querySelector('[data-active]');

observer = new ResizeObserver(() => {
  if (!indicator.isConnected || !indicator.hasAttribute('hidden')) {
    stop();
    return;
  }

  if (activeTab.offsetWidth > 0 && tabsList.offsetWidth > 0) {
    stop();
    position();
  }
});

activeTab is captured when the streamed HTML is parsed, but the delayed callback never verifies that it is still active. It also treats the indicator remaining hidden as proof that hydration has not taken ownership.

That assumption is incorrect. The hydrated component intentionally leaves the indicator hidden when the controlled value has no matching tab, and this behavior already has explicit test coverage. The observer can nevertheless call position() using the old server-side tab and remove hidden.

Failure scenario: The server renders tab "one" as selected inside a streamed boundary. Before the first ResizeObserver delivery, hydration or an immediate controlled update changes the value to "missing". React removes data-active and correctly keeps the indicator hidden. The callback still holds the old tab element, sees that it has a width, positions the indicator over it, and unhides it. With no subsequent resize or render, the stale indicator can remain visible indefinitely.

Fix: Revalidate ownership and selection before mutating:

const currentActiveTab = tabsList.querySelector('[data-active]');

if (currentActiveTab !== activeTab) {
  stop();
  return;
}

Capturing document.currentScript and stopping when that script is removed by hydration would provide an additional ownership signal. Re-querying the current active tab inside position() would be safer still.

2. 🟠 The observer disconnects before positioning has actually succeeded

Location: packages/react/src/tabs/indicator/prehydrationScript.template.js:102

if (activeTab.offsetWidth > 0 && tabsList.offsetWidth > 0) {
  stop();
  position();
}

The callback considers only width before permanently disconnecting, while position() only reveals the indicator when both width and height are positive. The observer also watches only tabsList, not activeTab.

Failure scenario: A streamed list has a fixed nonzero size, while an active icon-only tab initially has a CSS/flex width but zero height until its image or styles resolve. Revealing the segment makes the width check pass, so the observer disconnects. position() records a zero height and leaves the indicator hidden. When the tab later gains height without changing the fixed-size list, nothing is observed and the indicator stays hidden until hydration.

Fix: Have position() return whether it successfully revealed the indicator and only call stop() after success. Observe activeTab as well as tabsList, or require all relevant width and height measurements to be positive before disconnecting.

Tests (1)

1. 🟠 The new runtime observer path has no automated coverage

Location: packages/react/src/tabs/indicator/TabsIndicator.test.tsx:431

The new runtime observer path has no automated coverage. The existing pre-hydration tests verify only that the script is emitted, has contents and a nonce, survives hydration, and is subsequently removed. None executes the script or exercises its delayed ResizeObserver branch.

Failure scenario: Both bugs above, or a future regression where revealing the streamed segment no longer positions the indicator, pass the entire current test suite.

Fix: Add a Chromium test or E2E fixture using the real generated script that covers:

  1. A hidden streamed segment becoming visible before hydration.
  2. Hydration changing the selected value to one with no matching tab before observer delivery.
  3. The active tab becoming fully measurable after the list has already become visible.

Simplifications (0)

No findings.

Docs (0)

No findings.

Verdict

Request changes - the delayed observer can override hydrated state and display an indicator for a tab that is no longer active.


🤖 Review generated with Codex

@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch 3 times, most recently from 4b98341 to db5f00d Compare July 16, 2026 04:43
The ResizeObserver only self-disconnects from its own callback, which never
fires if the tabs subtree is removed while still 0×0. Arm a bounded timeout
alongside observe() so the observer can't retain the subtree indefinitely,
and clear it when the observer resolves early.
The delayed ResizeObserver in the pre-hydration script could override
state that hydration had already settled:

- It captured the server-rendered active tab and never revalidated it,
  so when hydration moved the selection (or selected a value with no
  matching tab, which intentionally keeps the indicator hidden), a late
  delivery would position the indicator over the stale tab and unhide it.
  The callback now bails once the captured tab loses `data-active`.
- It disconnected on a width-only check while positioning requires both
  width and height, permanently giving up on tabs that were not yet fully
  measurable. Positioning now reports success, the observer only stops
  after it, and it watches the active tab as well as the list so the tab
  gaining size on its own still triggers a delivery.

Add Chromium tests that execute the real generated script against
hand-built server markup, covering segment reveal, the stale-tab guard,
hydration taking ownership first, and a tab that becomes measurable late.

Assisted by Claude Fable 5
@brijeshb42
brijeshb42 force-pushed the tabs-indicator-prehydration-streaming branch from db5f00d to b8e1974 Compare July 16, 2026 04:44

@atomiks atomiks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PR review

No correctness regressions or merge-blocking issues found. The implementation is appropriately narrow overall, but one observer target appears unnecessary.

Bugs (0)

No findings.

Tests (0)

No findings.

Simplifications (1)

1. 🟡 Observe only the active tab

Location: packages/react/src/tabs/indicator/prehydrationScript.template.js:114

observer.observe(tabsList);
observer.observe(activeTab);

Observing tabsList appears redundant. updatePosition() returns false only while the captured active tab has zero width or height, and it can return true only after that tab’s box becomes measurable.

Revealing the streamed Suspense segment changes the active tab from 0×0 to its rendered dimensions, so observing activeTab already covers the primary case. It also covers the independently-delayed width or height case exercised by the new test.

Failure scenario: There is no behavioral failure, but each pre-hydration indicator installs an additional observation that cannot independently make the positioning retry succeed.

Fix: Remove the list observation and retain only:

observer.observe(activeTab);

The initial measurement, hydration guards, stale-selection guard, bounded timeout, and success-returning updatePosition() are otherwise all justified.

Docs (0)

No findings.

Verdict

Approve after nits - the implementation is correct, but the redundant tabsList observation can be removed to make the fix as small as possible.


🤖 Review generated with Codex

@brijeshb42
brijeshb42 merged commit 250f7fa into mui:master Jul 16, 2026
6 checks passed
@brijeshb42
brijeshb42 deleted the tabs-indicator-prehydration-streaming branch July 16, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: tabs Changes related to the tabs component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants