Repository navigation
perf(react-router): share one IntersectionObserver per router for viewport preloading - #9001
Conversation
🦋 Changeset detectedLatest commit: 95c7c4e The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
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 |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
View your CI Pipeline Execution ↗ for commit 95c7c4e
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
🚀 Changeset Version Preview1 package(s) bumped directly, 4 bumped as dependents. 🟩 Patch bumps
|
Bundle Size Benchmarks
The following scenarios have bundle-size changes compared with the baseline:
Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better. |
…wport preloading Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
162e78e to
95c7c4e
Compare
Merging this PR will regress 1 benchmark2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
95c7c4e452ccd5c21af84740c33c186eb95b358etest:unit,test:typesandtest:eslintpass.↑ New per-target routing test fails on the old per-Link code.
↑
link-viewportasserts one observe and one release per Link.↓ One intended behavior change: a leave + re-enter in one batch now restarts
preloadDelay.↓ No e2e run; CI pending.
↓
link-viewport(jsdom) shows no significant change.↓ Lab numbers come from a separate harness, not from CI.
↑ Solid and Vue are unchanged.
🎯 Changes
Before this change, every
<Link preload="viewport">created its ownIntersectionObserver. Now each router has one, stored inrouter.stores._linkObservernext to_linkScopes, and never in a module-level singleton. A moduleWeakMap<Element, handler>routes each entry to its Link's handler:enqueuePreloadandcancelPreloadin sequence ends in the same state as running only the last entry, with one exception: a leave + re-enter within one batch now restartspreloadDelay. The old code kept the existing timer. The test covers both cases.useStableValues, removed the stableinnerReffrom the hook deps, and madeenqueuePreload's entry parameter required, sinceentries.pop()no longer passesundefined.disconnectcounts, a new observer callback after a destination change) now assertunobserve.Performance
Motivating evidence (Chromium lab). A production bundle mounts and unmounts 1,000
<Link preload="viewport">underRouterProviderin Chromium. Results are from 12 interleaved rounds against the unpatched base, with an A/A control:defaultPreload'intent'falselink-viewportclient-nav scenario (#8998): no significant change. jsdom has noIntersectionObserver, so the scenario installs a stub whose constructor does almost nothing. That removes the native per-observer cost this change saves, so CI cannot show the improvement. These are local React runs on #8998's head (d9ea339), alternating baseline and candidate, in hz (higher is better):Both differences are well inside the baseline's own run-to-run spread. Solid and Vue are unchanged by this PR; their controls were 94.1 and 82.9 hz.
Bundle size (gzip bytes vs. #8998)
Known exception to the net-zero goal: gzip grows by a few bytes on every React scenario.
Alternatives I measured that were larger or no better:
WeakMapto the observer: +18 B[observer, Map]: +47 BWeakMapfor both roles: +7 to +19 BRoute/RootRoutemembers: −345 raw bytes, but no gzip gain✅ Checklist
CI=1 NX_DAEMON=false pnpm nx run-many -p @tanstack/react-router -t test:unit test:types test:eslint --skipRemoteCache: passedpnpm benchmark:bundle-size:run --baseline <#8998 baseline>: deltas abovelink-viewportReact/Solid/Vue viavitest bench --mode benchmark: numbers above🚀 Release Impact
Summary by CodeRabbit