Skip to content

perf(Breadcrumbs): rebuild overflow menu for fewer renders, cheaper layout and better SSR - #8683

Merged
mattcosta7 merged 9 commits into
mainfrom
mattcosta7-breadcrumbs-perf-rewrite
Nov 6, 2026
Merged

mattcosta7 merged 9 commits into
mainfrom
mattcosta7-breadcrumbs-perf-rewrite

Conversation

@mattcosta7

Copy link
Copy Markdown
Contributor

Closes #

Supersedes #8610.

Rebuilds overflow="menu" / "menu-with-root" so React renders only when the collapse point moves, crumbs never remount, and server-rendered markup is close to the hydrated result. There is no new public API and no feature flag.

How it works

  • One keyed <li> per crumb. Collapsing flips a data-collapsed attribute; it never remounts the crumb. A collapsed crumb that can come back at wider sizes stays in the row as a zero-size, invisible item, so scrollWidth still reports its natural width. Crumbs that can never come back aren't rendered.
  • Count rule during render. The "at most 4 visible (3 + root)" rule needs no measurement, so it applies during render, including on the server.
  • Measure once, resize from cache. Crumb widths are read once per children change and cached by key. The ResizeObserver recomputes from that cache, and React state changes only when the collapse point moves.
  • Lazy menu. The overflow menu's items and document listeners exist only while it's open. While it's open, collapsed crumbs render empty, so each crumb is in the DOM once.
  • Before hydration, CSS. Crumbs that don't fit are clipped at the start: the leaf stays visible and nothing paints outside the nav. A viewport media query applies the narrow (<544px) rule. JS sets data-hydrated in its first layout effect, so none of this applies afterwards.

Measurements

Production build with React Compiler, 100 instances (half menu, half menu-with-root, 7 crumbs), Chromium, median of 7 runs with implementations interleaved. Task time (ms):

Scenario CPU main This PR #8610, flag on
Mount 1× 61 46 57
Mount 4× 271 211 305
Parent re-render ×20 4× 1,246 176 487
Resize sweep, 101 steps 4× 1,197 975 2,470
Navigation ×4 4× 659 226 355

Resize layout time is on par with main (4×: 76ms vs 75ms). #8610's CSS path used 456ms.

SSR: server markup is rendered in the browser, then hydrated. The probe counts cases where visible crumbs, the menu, or the nav's size change on hydration. 220 cases per row: both modes, 2 to 7 crumbs, 160 to 1000px; viewport ≈ container width.

Container main This PR #8610, flag on
Block: changes after hydration 115 29 6
Block: crumbs painted outside the nav 50 0 0
Shrink-to-fit: renders nothing before hydration 0 0 220
Flex row with a sibling: changes 134 60 26

Changelog

New

  • Server-rendered menu / menu-with-root markup applies the count rule, clips crumbs that don't fit at the start, and applies the narrow rule on viewports under 544px.

Changed

  • Fewer renders on mount, resize, parent re-render and navigation (see above). Crumbs keep their DOM and state when the collapse point moves.
  • The overflow menu trigger is a <button>; it was a <summary> inside <details>. Its items and document listeners mount only while it's open.
  • The overflow menu no longer renders when it would be empty. main showed a "0 more breadcrumb items" button for 4-crumb menu-with-root and some 2-crumb cases.
  • Collapse points still use main's estimate (16px per crumb, 32px button) to keep visual parity. A sweep of 5,352 width × mode × variant × content combinations against main found 0 position differences. 13 combinations collapse one crumb up to 4px earlier; none are at VRT widths.

Removed

  • Nothing public.

Rollout strategy

  • No changes needed in github/github-ui
  • Needs changes in github/github-ui
  • Ships with feature flag(s)
  • Not sure
  • None

The global nav (ContextRegion, server-rendered by ui-service) is the main consumer. Its overflow test queries by role, so it should pass unchanged (inferred, not run). Please verify with the integration workflow.

Testing & Reviewing

Commands run:

  • npx vitest run --config vitest.config.browser.mts src/Breadcrumbs: passed, 40 tests
  • npx vitest run --config vitest.config.mts src/__tests__/ssr.test.tsx: passed, 4 tests
  • npm run type-check: passed
  • npx eslint packages/react/src/Breadcrumbs packages/react/src/__tests__/ssr.test.tsx --max-warnings=0: passed
  • npx stylelint packages/react/src/Breadcrumbs/Breadcrumbs.module.css: passed
  • npm run lint:react-compiler -w @primer/react: passed
  • npm run format:diff: passed
  • The Breadcrumbs browser suite also passes in WebKit.

New tests:

  • BreadcrumbsOverflow.test.tsx (real layout):
    • collapse geometry
    • no remounts
    • no duplicate crumbs while the menu is open
    • no empty menu
    • zero re-renders on resizes that don't cross a collapse point
    • a single render on mount
    • pre-hydration narrow rule and clipping, using real server markup
    • Every new test fails against main's implementation; the two pre-hydration tests also fail against this branch's first commit.
  • ssr.test.tsx: collapsed server markup and the hidden standby menu.

Known gaps:

  • Firefox not run: the browser wouldn't launch in my environment.
  • Linux VRT not run: CI will run it.
  • Remaining SSR changes: they come from width-based collapse, which CSS can't predict without perf(Breadcrumbs): remove render cascades; add CSS-driven overflow behind a feature flag #8610's scroll-timeline and wrap layout. That layout is what makes its runtime slower.
  • Nav wider than the viewport: the media query can show the narrow layout before hydration when JS won't (51/120 cases vs 48 on main).
  • Extra layout with many instances: the data-hydrated write runs before each instance's width read. When many instances mount in one commit, that adds layout passes. 100 instances at 4× take 211ms vs 194ms without it; one instance pays nothing extra (inferred).

Container queries were evaluated and rejected. They match the narrow rule exactly, but size containment renders content-sized containers (inline-block, grid auto columns) at 0px before hydration: 220/220 probe cases, also present in #8610.

mattcosta7 and others added 3 commits November 4, 2026 20:36
…ut work

Crumbs stay mounted as keyed list items, the count rule applies during render
(including SSR), widths are measured once per content change, and resizes only
re-render when the collapse point moves. The overflow menu mounts its items and
document listeners only while open.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…y menu, cheaper resize layout

- Collapsed crumbs render no content while the overflow menu shows them, so each crumb
  exists once in the DOM. Widths are cached by key so they survive that.
- Crumbs below the count rule at full width are not rendered at all.
- The overflow menu only renders when it has items (main showed an empty menu for
  4-crumb and some 2-crumb menu-with-root cases).
- Hidden measurable crumbs are zero-size in-flow flex items instead of absolute boxes,
  which cut resize layout time from ~1.7x to ~1.1x of main.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Server-rendered markup now clips crumbs that don't fit at the start (the leaf stays
visible, nothing paints outside the nav) and applies the narrow rule with a viewport
media query. JS sets data-hydrated in its first layout effect, so none of this applies
afterwards. A container query was rejected: its size containment renders content-sized
containers at 0px before hydration.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f5456b6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Check the integration testing docs for step-by-step instructions. Or, apply the integration-tests: skipped manually label to skip these checks.

@mattcosta7
mattcosta7 marked this pull request as ready for review November 5, 2026 03:23
@mattcosta7
mattcosta7 requested a review from a team as a code owner November 5, 2026 03:23
@mattcosta7
mattcosta7 requested review from tay1orjones and a balanced review from Copilot November 5, 2026 03:23
@mattcosta7 mattcosta7 added the Canary Release Apply this label when you want CI to create a canary release of the current PR label Nov 5, 2026

Copilot AI 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.

🟡 Changes recommended

Fit-dependent width remeasurement can repeatedly reverse collapse decisions near layout boundaries, causing a render loop that can hit React’s maximum update depth.

5 open issues
What changed in this PR

Rebuilds Breadcrumbs overflow handling to reduce renders, preserve crumb identity as collapse points change, and improve server-rendered layout without adding public API or a feature flag.

Changes:

  • Caches crumb widths and updates collapse state only when needed.
  • Uses a button-triggered overflow menu with lazily mounted content and listeners.
  • Adds pre-hydration layout rules and regression coverage for overflow, rendering, and SSR.
File Description
packages/​react/​src/​Breadcrumbs/​Breadcrumbs.tsx Reworks measurement, collapse state, keyed crumbs, and the overflow menu.
packages/​react/​src/​Breadcrumbs/​Breadcrumbs.module.css Adds collapsed-item styling and pre-hydration clipping and narrow-layout rules.
packages/​react/​src/​Breadcrumbs/​__tests__/​BreadcrumbsOverflow.test.tsx Adds real-layout, render-count, DOM identity, and hydration tests.
packages/​react/​src/​Breadcrumbs/​__tests__/​Breadcrumbs.test.tsx Updates menu selectors for the replacement markup.
packages/​react/​src/​Breadcrumbs/​__tests__/​__snapshots__/​BreadcrumbsItem.test.tsx.snap Refreshes the generated CSS class snapshot.
packages/​react/​src/​__tests__/​ssr.test.tsx Tests server-rendered collapse and the hidden standby menu.
.changeset/​breadcrumbs-overflow-performance.md Documents the patch release’s performance and behavior changes.

Review effort: Balanced

Copilot AI commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

One or more custom setup steps configured for this repository failed during this Copilot code review run:

Setup Playwright

Setup steps run before each review. If the review above is missing context, or no review was posted at all, the failing step above may be the cause. See the workflow run for failure details, fix your setup steps configuration, and re-request a review.

Note

You can configure setup steps for Copilot code review separately from Copilot cloud agent with a copilot-code-review.yml file. Read the docs for details.

mattcosta7 and others added 2 commits November 5, 2026 08:49
Real-layout repros: update-depth loop with large text, unhydrated Tab
focus offscreen, hide/show remeasure, content-box width, menuOpen reset.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Measure plain and collapsed separators independently, keep focused crumbs
visible via focus-within wrapping, preserve fit on zero-width hide/show,
use consistent content-box width, and reset menuOpen when wrapping.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A pointer left over the spot where the overflow menu button appears opens its tooltip, causing an unrelated render. Park the pointer there, then move it away before measuring.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mattcosta7 mattcosta7 self-assigned this Nov 5, 2026
@github-actions
github-actions Bot temporarily deployed to storybook-preview-8683 November 5, 2026 16:35 Inactive
@github-actions github-actions Bot added integration-tests: failing Changes in this PR cause breaking changes in gh/gh and removed integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm labels Nov 5, 2026

@tay1orjones tay1orjones 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.

LGTM but one note, it looks to me that the previous <details>/<summary> could open the disclosure before hydration, while the new button cannot

mattcosta7 and others added 2 commits November 5, 2026 18:20
…n't oscillate

A collapsed crumb sits in a zero-width grid item, so a crumb with a min-width
below its content shrank to that minimum and measured narrower than it is
when shown. The fit then flipped between the two states until React threw
'Maximum update depth exceeded'. Size the collapsed item's grid track to
max-content so the measurement is the natural width.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Preserve the overflow rewrite and natural-width measurement fix while adopting upstream browser locators and popup accessibility semantics.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@primer-integration

Copy link
Copy Markdown

Integration test results from github/github-ui PR:

Passed  Changes   Passed
Passed  CI   Passed
Passed  VRT   Passed
Passed  Projects   Passed

All checks passed!

@github-actions github-actions Bot added integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh and removed integration-tests: failing Changes in this PR cause breaking changes in gh/gh labels Nov 6, 2026
@mattcosta7
mattcosta7 added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 32d5061 Nov 6, 2026
56 checks passed
@mattcosta7
mattcosta7 deleted the mattcosta7-breadcrumbs-perf-rewrite branch November 6, 2026 23:49
@primer primer Bot mentioned this pull request Nov 6, 2026

This branch was successfully deployed

1 active and 1 inactive deployments
storybook-preview-8683 — f5456b62 Deployed Nov 6, 2026 by github-actions[bot]
github-pages — f5456b62 Deployed Nov 6, 2026 by mattcosta7 via deploy-preview #34148
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Canary Release Apply this label when you want CI to create a canary release of the current PR integration-tests: passing Changes in this PR do NOT cause breaking changes in gh/gh

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants