Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

perf(react-router): avoid Link's props copy and no-op preload handlers - #9005

Open
schiller-manuel wants to merge 2 commits into
schiller-manuel-perf-router-core-static-locationfrom
schiller-manuel-perf-react-link-no-props-copy
Open

schiller-manuel wants to merge 2 commits into
schiller-manuel-perf-router-core-static-locationfrom
schiller-manuel-perf-react-link-no-props-copy

Conversation

@schiller-manuel

@schiller-manuel schiller-manuel commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: c53dd283c40ae2b1b19fd3c447739a9ff57c627b

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ react-router test:unit, test:types and test:eslint pass at this revision.
↑ Tests pin each behavior change: element handlers in every preload mode, already-prevented events with and without intent, a key in state props, and defaultProps for function and class hosts. The handler and defaultProps tests fail on the base, as intended.
↑ The package build compiles <Host {...linkProps} /> to jsx(Host, { ...linkProps }), a copy, which justifies the direct jsx() call.
↑ React e2e apps with intent preloading (basic-file-based, basic-file-based-code-splitting) passed at 77b7102; not rerun since, because later changes touch only tests and comments.
↓ Calling jsx directly is compiler-facing rather than documented public API.
↓ Three user-visible behavior changes, listed in the PR body.
↓ Benchmark numbers are local wall-clock runs; CodSpeed CI results aren't inspected yet.

Stacked on #9003 (router-core static location slot). Base this review on that branch; it should land first.

🎯 Changes

Two React-only per-Link render savings:

  • No props copy. Link calls jsx from react/jsx-runtime directly and puts its children into linkProps. React 19's jsx uses a key-less config object as the element's props instead of copying it (facebook/react#28768). React 18 still copies, so it is neutral there. Plain JSX can't do this: this package's build (Vite 8, oxc automatic runtime) compiles <Host {...linkProps} /> to jsx(Host, { ...linkProps }), which copies. The direct call relies on the same invariant the compiler guarantees: linkProps is fresh per render, never cached, and never mutated after the call. Development builds freeze element.props.
  • No no-op preload handlers. onBlur, onFocus, onMouseEnter, onMouseLeave and onTouchStart are composed only for preload === 'intent'. In every other mode they did nothing, so the element's own handlers now pass through as given. The handlers no longer re-check the mode, and enqueuePreload no longer depends on preload.
const linkProps = useLinkProps(props, ref, host)
linkProps.children = typeof props.children === 'function' ? props.children({ isActive }) : props.children
return jsx(host, linkProps)

Behavior changes, each pinned by a test:

  • Already-prevented events: on Links without intent preloading, onFocus, onBlur, onMouseEnter, onMouseLeave and onTouchStart are attached as given. They now run even when event.defaultPrevented is already true, as on a plain <a>. Previously, composeHandlers skipped them. Intent Links still skip them.
  • defaultProps on custom hosts: React 19's jsx applies defaultProps only to class components. A function component passed to createLink no longer gets its defaultProps; createElement used to apply them. This matches React 19 JSX everywhere else. Use default parameters instead.
  • key in state props: a key in activeProps/inactiveProps is still the element's key, not a prop. React's development build now logs its "key spread into JSX" warning for it.

Client-nav benchmarks

Local vitest bench (jsdom, production builds), 6 interleaved base/candidate rounds on the same machine. Means are medians across rounds. Solid and Vue builds are byte-identical, so they are the noise controls.

Scenario Base mean This PR mean Δ mean Δ min
link-churn (React) 6.149 ms 5.102 ms −17.0% −10.4%
link-mount (React, defaultPreload: false) 3.101 ms 2.842 ms −8.4% −5.6%
link-mount intent (React) 2.928 ms 2.848 ms −2.8% −2.3%
link-mount (Solid, control) 8.137 ms 8.398 ms +3.2% +0.8%
link-mount intent (Solid, control) 8.284 ms 8.379 ms +1.2% +0.8%
link-mount (Vue, control) 20.121 ms 20.983 ms +4.3% +0.7%
link-mount intent (Vue, control) 20.824 ms 20.138 ms −3.3% −1.3%

The controls show about ±4% same-code noise. link-churn improved in 5 of 6 rounds and link-mount (React) in 5 of 6. The intent variant is within noise. Baseline is this PR's base branch.

Bundle size

Full benchmark:bundle-size run against the base branch (gzip bytes). react-router.minimal was re-measured after rebasing onto #9003's latest head; the other rows are from the previous base:

Scenario Δ gzip
react-router.minimal −39
react-router.full −27
react-start.minimal −20
react-start.full −23
react-start.query-integration −22
react-start.deferred-hydration −15
react-start.rsbuild.minimal −18
react-start.rsbuild.minimal-iife −20
react-start.rsbuild.full −15
Solid and Vue scenarios 0

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

Validation:

  • pnpm nx run-many -p @tanstack/react-router -t test:unit test:types test:eslint: passed
  • pnpm nx run-many -p tanstack-router-e2e-react-basic-file-based tanstack-router-e2e-react-basic-file-based-code-splitting -t test:e2e: passed
  • pnpm benchmark:bundle-size:run --baseline <base> (all scenarios): no growth, React −15 to −39 B gzip

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

AI-assisted: written with GitHub Copilot.

Summary by CodeRabbit

  • Bug Fixes
    • Link labels now remain unchanged when active or inactive state props are applied.
    • Links without intent preloading preserve their event handlers without triggering preloads. Intent preloading responds to focus, hover, and touch.
    • Links preserve custom event handlers for already-prevented events, except when intent preloading is enabled.
  • Compatibility
    • Improved link rendering compatibility with React 19. Function components used as custom link hosts should use default parameters instead of defaultProps.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Link component now creates elements with jsx from react/jsx-runtime and assigns resolved children to link props. It changes preload scheduling and attaches intent-specific event handlers only in intent mode. Tests cover event behavior, state props, and custom host defaults.

Changes: Link behavior

Layer / File(s) Summary
Link rendering and children
packages/react-router/src/link.tsx, packages/react-router/tests/link-state-props.test.tsx
Link uses jsx and assigns resolved children to link props. Tests cover explicit children, keys from active state props, and defaultProps behavior for function and class link hosts.
Preload scheduling and event handlers
packages/react-router/src/link.tsx, packages/react-router/tests/link-events.test.tsx, .changeset/react-link-no-props-copy.md
Non-intersecting observer entries cancel pending preload timers. Intent event handlers attach only in intent mode. Tests cover supplied handlers and preload calls across preload modes. The changeset documents the patch and these behaviors.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • TanStack/router#8999: Adds React, Solid, and Vue link-mount benchmark scenarios that provide context for the reported Link mount measurements.

Suggested reviewers: sheraff

Merge Risk: Minimal · up to c53dd

This change reduces Link render overhead and keeps the handler and children behavior covered by new tests. The documented behavior differences are intentional and noted in a changeset. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two main changes: avoiding Link's props copy and removing no-op preload handlers. It follows the conventional perf(scope) format and matches the changeset, link.tsx changes, and PR…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template closely. It keeps the Changes, Checklist, and Release Impact sections and checks the boxes. It also discloses AI authorship and explains the change in d…
Full details: Title check

Explanation

The title names the two main changes: avoiding Link's props copy and removing no-op preload handlers. It follows the conventional perf(scope) format and matches the changeset, link.tsx changes, and PR objectives. It is 70 characters, within a typical length limit.

Full details: Description check

Explanation

The description follows the repository template closely. It keeps the Changes, Checklist, and Release Impact sections and checks the boxes. It also discloses AI authorship and explains the change in detail. It covers the three user-visible behavior changes, the React 19 jsx rationale, validation commands, and benchmark and bundle-size results. Its Changes section is long and includes a self-assessment table that the template does not ask for. The template's terse-prose guidance favors shorter wording. The description also states that the PR is stacked on #9003, which reviewers need to know. Overall, the required information is present and complete.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 48e72ff and c53dd28.

Files selected for processing: 4

  • .changeset/react-link-no-props-copy.md
  • packages/react-router/src/link.tsx
  • packages/react-router/tests/link-events.test.tsx
  • packages/react-router/tests/link-state-props.test.tsx
Files skipped from review as they are similar to previous changes: 2

  • .changeset/react-link-no-props-copy.md
  • packages/react-router/src/link.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c53dd28

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

This PR includes changesets to release 5 packages
Name Type
@tanstack/react-router Patch
@tanstack/react-start-client Patch
@tanstack/react-start-rsc Patch
@tanstack/react-start-server Patch
@tanstack/react-start 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

@nx-cloud

nx-cloud Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit c53dd28

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 9m 27s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 5s View ↗

💡 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 2026-11-27 17:05:38 UTC

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

2 package(s) bumped directly, 21 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-router 1.170.46 → 1.170.47 Changeset
@tanstack/router-core 1.171.39 → 1.171.40 Changeset
@tanstack/react-start 1.169.7 → 1.169.8 Dependent
@tanstack/react-start-client 1.168.45 → 1.168.46 Dependent
@tanstack/react-start-rsc 0.1.68 → 0.1.69 Dependent
@tanstack/react-start-server 1.167.52 → 1.167.53 Dependent
@tanstack/router-cli 1.168.0 → 1.168.1 Dependent
@tanstack/router-generator 1.168.0 → 1.168.1 Dependent
@tanstack/router-plugin 1.169.0 → 1.169.1 Dependent
@tanstack/router-vite-plugin 1.167.49 → 1.167.50 Dependent
@tanstack/solid-router 1.170.43 → 1.170.44 Dependent
@tanstack/solid-start 1.169.7 → 1.169.8 Dependent
@tanstack/solid-start-client 1.168.42 → 1.168.43 Dependent
@tanstack/solid-start-server 1.167.50 → 1.167.51 Dependent
@tanstack/start-client-core 1.170.39 → 1.170.40 Dependent
@tanstack/start-plugin-core 1.172.0 → 1.172.1 Dependent
@tanstack/start-server-core 1.169.45 → 1.169.46 Dependent
@tanstack/start-static-server-functions 1.167.44 → 1.167.45 Dependent
@tanstack/start-storage-context 1.167.41 → 1.167.42 Dependent
@tanstack/vue-router 1.170.42 → 1.170.43 Dependent
@tanstack/vue-start 1.169.7 → 1.169.8 Dependent
@tanstack/vue-start-client 1.167.45 → 1.167.46 Dependent
@tanstack/vue-start-server 1.167.50 → 1.167.51 Dependent

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: c3fd85fe81b2
  • Measured at: 2026-11-27T16:57:13.978Z
  • Baseline source: history:10b22c276ea7
  • Dashboard: bundle-size history

The following scenarios have bundle-size changes compared with the baseline:

Scenario Current (gzip) Initial (gzip) Raw Brotli Trend
react-router.minimal 84.0 KiB
-67 B
83.8 KiB
-65 B
261.3 KiB
-195 B
73.3 KiB
-10 B
▆▆▆▆▆▆▆████▁
react-router.full 87.5 KiB
-54 B
87.4 KiB
-57 B
273.2 KiB
-192 B
76.4 KiB
-68 B
▅▅▅▅▅▅▅████▁
solid-router.minimal 33.4 KiB
-23 B
33.2 KiB
-24 B
95.2 KiB
-136 B
30.2 KiB
-19 B
▁▁▁▁▁▁▁████▅
solid-router.full 38.2 KiB
-41 B
38.1 KiB
-40 B
110.0 KiB
-133 B
34.5 KiB
-61 B
▅▅▅▅▅▅▅████▁
vue-router.minimal 49.5 KiB
-12 B
49.3 KiB
-11 B
136.4 KiB
-133 B
44.9 KiB
+63 B
▁▁▁▁▁▁▁████▆
vue-router.full 55.0 KiB
-24 B
54.9 KiB
-24 B
154.5 KiB
-133 B
49.6 KiB
-38 B
▁▁▁▁▁▁▁████▅
react-start.minimal 96.8 KiB
-47 B
96.6 KiB
-47 B
302.3 KiB
-188 B
84.0 KiB
-142 B
▇▇▇▇▇▇▇████▁
react-start.query-integration 104.1 KiB
-71 B
104.0 KiB
-72 B
328.8 KiB
-188 B
90.5 KiB
+3 B
▇▇▇▇▇▇▇████▁
react-start.deferred-hydration 97.5 KiB
-59 B
96.6 KiB
-64 B
303.6 KiB
-188 B
84.8 KiB
-29 B
▄▄▄▄▄▅▅████▁
react-start.full 99.8 KiB
-66 B
99.7 KiB
-65 B
311.5 KiB
-182 B
86.6 KiB
-49 B
▇▇▇▇▇▇▇████▁
react-start.rsbuild.minimal 96.6 KiB
-40 B
96.5 KiB
-40 B
298.1 KiB
-189 B
83.4 KiB
-22 B
██▁▁▁▁▁▁▁▁▁▁
react-start.rsbuild.minimal-iife 97.0 KiB
-38 B
96.9 KiB
-38 B
299.1 KiB
-189 B
83.8 KiB
-10 B
██▁▁▁▁▁▁▁▁▁▁
react-start.rsbuild.full 99.9 KiB
-43 B
99.8 KiB
-43 B
307.7 KiB
-189 B
86.1 KiB
+67 B
██▁▁▁▁▁▁▁▁▁▁
solid-start.minimal 46.2 KiB
-35 B
46.1 KiB
-35 B
135.0 KiB
-133 B
41.1 KiB
-121 B
▁▁▁▁▁▁▁████▅
solid-start.deferred-hydration 49.2 KiB
-36 B
46.1 KiB
-36 B
142.3 KiB
-133 B
43.9 KiB
-80 B
▁▁▁▁▁▁▁████▃
solid-start.full 51.2 KiB
-27 B
51.1 KiB
-24 B
150.0 KiB
-133 B
45.5 KiB
+56 B
▁▁▁▁▁▁▁████▅
vue-start.minimal 65.4 KiB
-27 B
65.3 KiB
-25 B
185.6 KiB
-133 B
58.4 KiB
-17 B
▁▁▁▁▁▁▁████▄
vue-start.full 69.1 KiB
-21 B
69.0 KiB
-21 B
197.5 KiB
-133 B
61.5 KiB
-126 B
▁▁▁▁▁▁▁████▆

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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@9005

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@9005

@tanstack/eslint-plugin-start

npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@9005

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@9005

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@9005

@tanstack/react-router-devtools

npm i https://pkg.pr.new/@tanstack/react-router-devtools@9005

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@9005

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@9005

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@9005

@tanstack/react-start-rsc

npm i https://pkg.pr.new/@tanstack/react-start-rsc@9005

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@9005

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@9005

@tanstack/router-core

npm i https://pkg.pr.new/@tanstack/router-core@9005

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@9005

@tanstack/router-devtools-core

npm i https://pkg.pr.new/@tanstack/router-devtools-core@9005

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@9005

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@9005

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@9005

@tanstack/router-utils

npm i https://pkg.pr.new/@tanstack/router-utils@9005

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@9005

@tanstack/solid-router

npm i https://pkg.pr.new/@tanstack/solid-router@9005

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/@tanstack/solid-router-devtools@9005

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@9005

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@9005

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@9005

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@9005

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@9005

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@9005

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@9005

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@9005

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/@tanstack/start-static-server-functions@9005

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@9005

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@9005

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@9005

@tanstack/vue-router

npm i https://pkg.pr.new/@tanstack/vue-router@9005

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/@tanstack/vue-router-devtools@9005

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@9005

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@9005

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@9005

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@9005

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@9005

commit: c53dd28

Link renders through react/jsx-runtime's jsx, so React 19 uses its fresh
props object for the element instead of copying it. Focus, hover and touch
handlers are attached only for intent preloading; in other modes they were
no-ops, and the element's own handlers pass through unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@schiller-manuel
schiller-manuel force-pushed the schiller-manuel-perf-react-link-no-props-copy branch from 77b7102 to 48e72ff Compare November 27, 2026 15:16
@codspeed

codspeed Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

2 warnings

⚠️ 39 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 5 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 7 improved benchmarks
❌ 1 regressed benchmark
✅ 192 untouched benchmarks
⏩ 3 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory mem client interrupted-navigations (solid) 84.4 KB 87.1 KB -3.08%
⚡ Memory mem client navigation-churn (solid) 113.1 KB 102 KB +10.93%
⚡ CPU Simulation client-link-churn navigation loop (react) 245.7 ms 228.7 ms +7.42%
⚡ CPU Simulation client-link-mount loop (react) 176.4 ms 166.4 ms +6.03%
⚡ Memory mem server error-paths redirect (react) 14.1 KB 13.4 KB +4.78%
⚡ Memory mem server error-paths redirect (solid) 11.5 KB 11 KB +4.69%
⚡ Memory mem client unique-location-churn (vue) 83.8 KB 80.6 KB +3.99%
⚡ CPU Simulation client-search-params navigation loop (react) 120 ms 116.5 ms +3.02%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing schiller-manuel-perf-react-link-no-props-copy (c53dd28) with schiller-manuel-perf-router-core-static-location (2c725bd)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

…ith jsx

Document why Link calls jsx() directly: compiled JSX copies the spread
props. Pin that non-intent Links attach element handlers as given, that a
state-prop key stays the element key, and that React 19's jsx resolves
defaultProps only for class hosts.

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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants