fix: don't re-execute stale loaders on unrelated location changes #6867
main fromrefs/pull/6867/head·merged by schiller-manuel(GitHub profile, opens in a new tab) Summary by CodeRabbit#
New Features
- Added ability to forcefully reload stale matches via explicit router.load() calls.
Bug Fixes
- Stale-match revalidation now occurs when routes are re-entered, loader dependencies change, or router.load() is called; background reload behavior made more consistent.
Documentation
- Clarified Router.load() semantics and updated staleTime guidance.
Tests
- Updated unit and end-to-end tests and expectations (including separating loader vs. context checks) to reflect the revised reload behavior.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsdocumentation
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: vue-router
View your CI Pipeline Execution ↗ for commit 79eec144aa0efe473de0951bf962e1d4f01e8c77
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...✅ Succeeded 11m 56s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 2m 2s View ↗
☁️ Nx Cloud last updated this comment at
2026-03-08 23:13:41UTCBundle Size Benchmarks#
- Commit:
88fcbec9865c - Measured at:
2026-03-08T23:02:34.668Z - Baseline source:
history:7003ccb50f7e - Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Raw Brotli Trend react-router.minimal87.01 KiB +170 B (+0.19%) 273.70 KiB 75.55 KiB ▁▁▅▅▅▅▅▅▅▅▅█ react-router.full90.00 KiB +124 B (+0.13%) 283.96 KiB 78.37 KiB ▁▁▆▆▆▆▆▆▆▆▆█ solid-router.minimal36.32 KiB +146 B (+0.39%) 108.81 KiB 32.63 KiB ▁▁▆▆▆▆▆▆▆▆▆█ solid-router.full40.61 KiB +110 B (+0.27%) 121.79 KiB 36.46 KiB ▁▁▆▆▆▆▆▆▆▆▆█ vue-router.minimal52.17 KiB +135 B (+0.25%) 148.83 KiB 46.92 KiB ▁▁▆▆▆▆▆▆▆▆▆█ vue-router.full56.96 KiB +119 B (+0.20%) 164.34 KiB 51.11 KiB ▁▁▆▆▆▆▆▆▆▆▆█ react-start.minimal99.54 KiB +143 B (+0.14%) 312.85 KiB 86.11 KiB ▁▁▆▆▆▆▆▆▆▆▆█ react-start.full102.90 KiB +116 B (+0.11%) 322.58 KiB 88.98 KiB ▁▁▆▆▆▆▆▆▆▆▆█ solid-start.minimal48.63 KiB +141 B (+0.28%) 146.40 KiB 43.07 KiB ▁▁▆▆▆▆▆▆▆▆▆█ solid-start.full54.09 KiB +133 B (+0.24%) 162.27 KiB 47.67 KiB ▁▁▆▆▆▆▆▆▆▆▆█ Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better.
- Commit:
More templates
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-router-react-example-basic-authjs
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-rsc
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-streaming-data-from-server-functions
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
- tanstack-router-vue-example-basic-jsx
- tanstack-router-vue-example-basic-file-based-jsx
- tanstack-router-vue-example-basic-file-based-sfc
- tanstack-router-solid-example-authenticated-routes
- tanstack-router-solid-example-authenticated-routes-firebase
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-default-search-params
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-basic-virtual-file-based
- tanstack-router-solid-example-basic-virtual-inside-file-based
- tanstack-router-solid-example-deferred-data
- tanstack-router-solid-i18n-paraglide
- tanstack-router-solid-example-kitchen-sink
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-kitchen-sink-solid-query
- tanstack-router-solid-example-kitchen-sink-solid-query-file-based
- tanstack-router-solid-example-large-file-based
- tanstack-router-solid-example-location-masking
- tanstack-router-solid-example-navigation-blocking
- tanstack-router-solid-example-quickstart
- tanstack-router-solid-example-quickstart-esbuild-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-router-solid-example-quickstart-rspack-file-based
- tanstack-router-solid-example-quickstart-webpack-file-based
- @tanstack/router-solid-mono-simple
- router-solid-mono-simple-lazy
- router-solid-monorepo-solid-query
- tanstack-router-solid-example-scroll-restoration
- tanstack-solid-router-search-validator-adapters
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-auth
- tanstack-solid-start-example-basic-authjs
- tanstack-solid-start-example-basic-cloudflare
- tanstack-solid-start-example-basic-netlify
- tanstack-solid-start-example-basic-nitro
- tanstack-start-example-basic-solid-query
- tanstack-solid-start-example-basic-static
- tanstack-solid-start-bun-hosting
- tanstack-solid-start-example-convex-better-auth
- tanstack-solid-start-example-counter
- tanstack-solid-start-i18n-paraglide
- tanstack-solid-start-example-large
- tanstack-solid-start-streaming-data-from-server-functions
- tanstack-solid-start-example-supabase-basic
- tanstack-solid-start-tailwind-v4
- tanstack-router-solid-example-view-transitions
- tanstack-router-solid-example-with-framer-motion
- tanstack-router-solid-example-with-trpc
@tanstack/arktype-adapter
npm i https://pkg.pr.new/@tanstack/arktype-adapter@6867@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@6867@tanstack/history
npm i https://pkg.pr.new/@tanstack/history@6867@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@6867@tanstack/react-router
npm i https://pkg.pr.new/@tanstack/react-router@6867@tanstack/react-router-devtools
npm i https://pkg.pr.new/@tanstack/react-router-devtools@6867@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@6867@tanstack/react-start
npm i https://pkg.pr.new/@tanstack/react-start@6867@tanstack/react-start-client
npm i https://pkg.pr.new/@tanstack/react-start-client@6867@tanstack/react-start-server
npm i https://pkg.pr.new/@tanstack/react-start-server@6867@tanstack/router-cli
npm i https://pkg.pr.new/@tanstack/router-cli@6867@tanstack/router-core
npm i https://pkg.pr.new/@tanstack/router-core@6867@tanstack/router-devtools
npm i https://pkg.pr.new/@tanstack/router-devtools@6867@tanstack/router-devtools-core
npm i https://pkg.pr.new/@tanstack/router-devtools-core@6867@tanstack/router-generator
npm i https://pkg.pr.new/@tanstack/router-generator@6867@tanstack/router-plugin
npm i https://pkg.pr.new/@tanstack/router-plugin@6867@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@6867@tanstack/router-utils
npm i https://pkg.pr.new/@tanstack/router-utils@6867@tanstack/router-vite-plugin
npm i https://pkg.pr.new/@tanstack/router-vite-plugin@6867@tanstack/solid-router
npm i https://pkg.pr.new/@tanstack/solid-router@6867@tanstack/solid-router-devtools
npm i https://pkg.pr.new/@tanstack/solid-router-devtools@6867@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@6867@tanstack/solid-start
npm i https://pkg.pr.new/@tanstack/solid-start@6867@tanstack/solid-start-client
npm i https://pkg.pr.new/@tanstack/solid-start-client@6867@tanstack/solid-start-server
npm i https://pkg.pr.new/@tanstack/solid-start-server@6867@tanstack/start-client-core
npm i https://pkg.pr.new/@tanstack/start-client-core@6867@tanstack/start-fn-stubs
npm i https://pkg.pr.new/@tanstack/start-fn-stubs@6867@tanstack/start-plugin-core
npm i https://pkg.pr.new/@tanstack/start-plugin-core@6867@tanstack/start-server-core
npm i https://pkg.pr.new/@tanstack/start-server-core@6867@tanstack/start-static-server-functions
npm i https://pkg.pr.new/@tanstack/start-static-server-functions@6867@tanstack/start-storage-context
npm i https://pkg.pr.new/@tanstack/start-storage-context@6867@tanstack/valibot-adapter
npm i https://pkg.pr.new/@tanstack/valibot-adapter@6867@tanstack/virtual-file-routes
npm i https://pkg.pr.new/@tanstack/virtual-file-routes@6867@tanstack/vue-router
npm i https://pkg.pr.new/@tanstack/vue-router@6867@tanstack/vue-router-devtools
npm i https://pkg.pr.new/@tanstack/vue-router-devtools@6867@tanstack/vue-router-ssr-query
npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@6867@tanstack/vue-start
npm i https://pkg.pr.new/@tanstack/vue-start@6867@tanstack/vue-start-client
npm i https://pkg.pr.new/@tanstack/vue-start-client@6867@tanstack/vue-start-server
npm i https://pkg.pr.new/@tanstack/vue-start-server@6867@tanstack/zod-adapter
npm i https://pkg.pr.new/@tanstack/zod-adapter@6867commit:
03cf375Actionable comments posted: 6
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
packages/router-core/src/router.ts (1)
2375-2407: ⚠️ Potential issue | 🟠 Majorhrefequality is too broad for forced stale reloads.previousLocation.href === next.hrefis also true for same-URL state/history transitions, so those paths will still force-revalidate stale loaders even when the loader key is unchanged. That reintroduces the same class of unrelated-change reloads this PR is trying to remove. Please thread an explicit opt-in from the call sites that intentionally revalidate instead of deriving it from location equality here. A regression test for same-href state-only navigation would lock this down.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/router-core/src/router.ts` around lines 2375 - 2407, The current forced stale reload decision uses previousLocation.href === next.href which incorrectly treats same-URL state/history navigations as a forced reload; change loadMatches call to rely on an explicit opt-in flag (e.g., opts?.forceStaleReload) instead of deriving it from href equality, update the loadMatches parameter name if needed to match the explicit flag (forceStaleReload) and update all call sites that intentionally want to force revalidation to pass that flag, and add a regression test for a same-href state-only navigation (no loader-key change) to assert loaders are not revalidated.packages/router-core/src/load-matches.ts (1)
4-4: ⚠️ Potential issue | 🟠 MajorAvoid unconditional store writes in
syncMatchContext.buildMatchContext()always allocates, so this helper currently emits a match update even when the merged context is unchanged. That turns every stale-skip path into extra store churn/re-renders, which is why the navigation update expectations had to move up in this PR.Suggested fix
-import { createControlledPromise, isPromise } from './utils' +import { createControlledPromise, isPromise, replaceEqualDeep } from './utils'const syncMatchContext = ( inner: InnerLoadContext, matchId: string, index: number, ): void => { const nextContext = buildMatchContext(inner, index) inner.updateMatch(matchId, (prev) => { + const context = replaceEqualDeep(prev.context, nextContext) + if (context === prev.context) { + return prev + } + return { ...prev, - context: nextContext, + context, } }) }Also applies to: 201-214
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/router-core/src/load-matches.ts` at line 4, syncMatchContext currently always writes the merged context created by buildMatchContext, causing unnecessary store updates; change it to first compute the merged context (call buildMatchContext as now), then compare the merged context to the existing match.context (use the same equality semantics the store expects—shallow/deep as appropriate), and only perform the store write/update when they differ; update the code paths in syncMatchContext (and the similar block at lines ~201-214) to early-return when contexts are equal to avoid unconditional store writes.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/react-router/src/fileRoute.ts`: - Around line 232-233: The helper LazyRoute.useRouteContext currently lets callers override the bound route by spreading opts after from; change the call so the route ID wins by applying opts first and then setting from to this.options.id (i.e., ensure the object passed to useRouteContext has from: this.options.id after spreading opts), so the bound route cannot be overridden; update the useRouteContext invocation in LazyRoute.useRouteContext accordingly. In `@packages/react-router/src/route.tsx`: - Around line 117-119: The bound helper useRouteContext currently spreads opts after specifying from which lets callers override this.id at runtime; change the call in the useRouteContext: UseRouteContextRoute<TId> implementation so that from: this.id is placed before ...opts is spread (i.e., ensure from is first and opts merged after it is not allowed to override it), and make the identical ordering change for the RouteApi and RootRoute bound variants referenced in the comment (the other wrappers at the same pattern) so all route-bound hooks keep from pinned to this.id. In `@packages/solid-router/src/fileRoute.ts`: - Around line 220-221: The helper currently spreads opts after setting from, which allows callers to override the bound route id; change the call inside LazyRoute.useRouteContext to ensure the route id is pinned by making from take precedence (e.g., pass { ...(opts as any), from: this.options.id } to useRouteContext) so useRouteContext always uses this.options.id and cannot be overridden by opts. In `@packages/solid-router/src/route.tsx`: - Around line 107-109: The route-bound wrapper useRouteContext: UseRouteContextRoute<TId> currently spreads opts after from so callers can override the bound route id; change the call to ensure from is fixed by placing from last (e.g. call useRouteContext({ ...(opts as any), from: this.id as any }) ) or otherwise prevent opts from overwriting from; apply the same fix to the other two route-bound wrappers that follow the same pattern so the bound route id cannot be replaced by callers. In `@packages/vue-router/src/fileRoute.ts`: - Around line 220-221: The helper currently allows callers to override the bound route because it spreads opts after setting from; change the merge so the bound route id wins. In LazyRoute.useRouteContext (the useRouteContext property) call useRouteContext({ ...(opts as any), from: this.options.id }) or otherwise ensure opts cannot override from (e.g. remove opts.from before merging) so the returned context is always pinned to this.options.id. In `@packages/vue-router/src/route.ts`: - Around line 108-110: The bound helper currently spreads opts after setting from, allowing a stray opts.from to override the route id; change the object ordering so opts are spread first and from is set last (e.g., call useRouteContext({ ...(opts as any), from: this.id as any })), and apply the same fix to the RouteApi and RootRoute bound variants referenced elsewhere so their from property cannot be overridden by opts. --- Outside diff comments: In `@packages/router-core/src/load-matches.ts`: - Line 4: syncMatchContext currently always writes the merged context created by buildMatchContext, causing unnecessary store updates; change it to first compute the merged context (call buildMatchContext as now), then compare the merged context to the existing match.context (use the same equality semantics the store expects—shallow/deep as appropriate), and only perform the store write/update when they differ; update the code paths in syncMatchContext (and the similar block at lines ~201-214) to early-return when contexts are equal to avoid unconditional store writes. In `@packages/router-core/src/router.ts`: - Around line 2375-2407: The current forced stale reload decision uses previousLocation.href === next.href which incorrectly treats same-URL state/history navigations as a forced reload; change loadMatches call to rely on an explicit opt-in flag (e.g., opts?.forceStaleReload) instead of deriving it from href equality, update the loadMatches parameter name if needed to match the explicit flag (forceStaleReload) and update all call sites that intentionally want to force revalidation to pass that flag, and add a regression test for a same-href state-only navigation (no loader-key change) to assert loaders are not revalidated.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
1eeae827-f075-41a8-8c79-112ff22bddac📥 Commits
Reviewing files that changed from the base of the PR and between 88fcbec9865cca6bf9ec638b88a9c4fb2c8310a8 and 68f4530412219830bd07ac664a0d3c8342b14727.
📒 Files selected for processing (15)
docs/router/api/router/RouterType.mddocs/router/guide/data-loading.mdpackages/react-router/src/fileRoute.tspackages/react-router/src/route.tsxpackages/react-router/tests/store-updates-during-navigation.test.tsxpackages/router-core/src/Matches.tspackages/router-core/src/load-matches.tspackages/router-core/src/router.tspackages/router-core/tests/load.test.tspackages/solid-router/src/fileRoute.tspackages/solid-router/src/route.tsxpackages/solid-router/tests/store-updates-during-navigation.test.tsxpackages/vue-router/src/fileRoute.tspackages/vue-router/src/route.tspackages/vue-router/tests/store-updates-during-navigation.test.tsx
packages/react-router/src/fileRoute.ts lines 232 to 233OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -229,7 +230,4 @@229230 }230231231232 useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {232- return useMatch({233- from: this.options.id,234- select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),235- }) as any233+ return useRouteContext({ from: this.options.id, ...(opts as any) }) as any⚠️ Potential issue | 🟡 Minor
Keep
LazyRoute.useRouteContext()pinned to its route ID.Spreading
optsafterfromlets JS/anycallers override the bound route and read another match's context through this helper. The other route-bound helpers in this class force their ownfrom; this should too.Suggested fix
- return useRouteContext({ from: this.options.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.options.id }) as any🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-router/src/fileRoute.ts` around lines 232 - 233, The helper LazyRoute.useRouteContext currently lets callers override the bound route by spreading opts after from; change the call so the route ID wins by applying opts first and then setting from to this.options.id (i.e., ensure the object passed to useRouteContext has from: this.options.id after spreading opts), so the bound route cannot be overridden; update the useRouteContext invocation in LazyRoute.useRouteContext accordingly.✅ Addressed in commits e42b833 to f4140b5
packages/react-router/src/route.tsx lines 117 to 119Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -114,8 +115,5 @@114115 }115116116117 useRouteContext: UseRouteContextRoute<TId> = (opts) => {117- return useMatch({118- from: this.id as any,119- select: (d) => (opts?.select ? opts.select(d.context) : d.context),120- }) as any118+ return useRouteContext({ from: this.id as any, ...(opts as any) }) as any121119 }⚠️ Potential issue | 🟡 Minor
Keep
fromfixed in the bounduseRouteContexthelpers.These wrappers are supposed to stay pinned to
this.id, but spreadingoptsafterfromallows a runtime override and makes them behave differently from the other route-bound hooks in this file.Suggested fix
- return useRouteContext({ from: this.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.id }) as anyApply the same ordering to the
RouteApiandRootRoutevariants too.Also applies to: 272-274, 544-546
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-router/src/route.tsx` around lines 117 - 119, The bound helper useRouteContext currently spreads opts after specifying from which lets callers override this.id at runtime; change the call in the useRouteContext: UseRouteContextRoute<TId> implementation so that from: this.id is placed before ...opts is spread (i.e., ensure from is first and opts merged after it is not allowed to override it), and make the identical ordering change for the RouteApi and RootRoute bound variants referenced in the comment (the other wrappers at the same pattern) so all route-bound hooks keep from pinned to this.id.✅ Addressed in commits e42b833 to f4140b5
packages/solid-router/src/fileRoute.ts lines 220 to 221OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -217,7 +218,4 @@217218 }218219219220 useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {220- return useMatch({221- from: this.options.id,222- select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),223- }) as any221+ return useRouteContext({ from: this.options.id, ...(opts as any) }) as any⚠️ Potential issue | 🟡 Minor
Keep
LazyRoute.useRouteContext()pinned to its route ID.Spreading
optsafterfromlets JS/anycallers override the bound route and fetch the wrong context through this helper. The other route-bound helpers in this class keep their ownfrom; this should match that behavior.Suggested fix
- return useRouteContext({ from: this.options.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.options.id }) as any📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change220- useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {221- return useRouteContext({ from: this.options.id, ...(opts as any) }) as any220+ useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {221+ return useRouteContext({ ...(opts as any), from: this.options.id }) as any🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/solid-router/src/fileRoute.ts` around lines 220 - 221, The helper currently spreads opts after setting from, which allows callers to override the bound route id; change the call inside LazyRoute.useRouteContext to ensure the route id is pinned by making from take precedence (e.g., pass { ...(opts as any), from: this.options.id } to useRouteContext) so useRouteContext always uses this.options.id and cannot be overridden by opts.✅ Addressed in commits e42b833 to f4140b5
packages/solid-router/src/route.tsx lines 107 to 109Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -104,8 +105,5 @@104105 }105106106107 useRouteContext: UseRouteContextRoute<TId> = (opts) => {107- return useMatch({108- from: this.id as any,109- select: (d) => (opts?.select ? opts.select(d.context) : d.context),110- }) as any108+ return useRouteContext({ from: this.id as any, ...(opts as any) }) as any111109 }⚠️ Potential issue | 🟡 Minor
Don't let the route-bound
useRouteContext()helpers overridefrom.All three helpers now spread
optsafterfrom, so JS/anycallers can replace the bound route ID and read the wrong context. These route-bound wrappers should keepfromfixed, like the other helpers in this file.Suggested fix
- return useRouteContext({ from: this.id as any, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.id as any }) as any- return useRouteContext({ from: this.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.id }) as any- return useRouteContext({ from: this.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.id }) as anyAlso applies to: 255-257, 493-495
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/solid-router/src/route.tsx` around lines 107 - 109, The route-bound wrapper useRouteContext: UseRouteContextRoute<TId> currently spreads opts after from so callers can override the bound route id; change the call to ensure from is fixed by placing from last (e.g. call useRouteContext({ ...(opts as any), from: this.id as any }) ) or otherwise prevent opts from overwriting from; apply the same fix to the other two route-bound wrappers that follow the same pattern so the bound route id cannot be replaced by callers.✅ Addressed in commits e42b833 to f4140b5
packages/vue-router/src/fileRoute.ts lines 220 to 221OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -217,7 +218,4 @@217218 }218219219220 useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {220- return useMatch({221- from: this.options.id,222- select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),223- }) as any221+ return useRouteContext({ from: this.options.id, ...(opts as any) }) as any⚠️ Potential issue | 🟡 Minor
Keep
LazyRoute.useRouteContext()pinned to its route ID.Spreading
optsafterfromlets JS/anycallers override the bound route and fetch the wrong context through this helper. The other route-bound helpers in this class keep their ownfrom; this should match that behavior.Suggested fix
- return useRouteContext({ from: this.options.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.options.id }) as any📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
Suggested change220- useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {221- return useRouteContext({ from: this.options.id, ...(opts as any) }) as any220+ useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {221+ return useRouteContext({ ...(opts as any), from: this.options.id }) as any🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/vue-router/src/fileRoute.ts` around lines 220 - 221, The helper currently allows callers to override the bound route because it spreads opts after setting from; change the merge so the bound route id wins. In LazyRoute.useRouteContext (the useRouteContext property) call useRouteContext({ ...(opts as any), from: this.options.id }) or otherwise ensure opts cannot override from (e.g. remove opts.from before merging) so the returned context is always pinned to this.options.id.✅ Addressed in commits e42b833 to f4140b5
packages/vue-router/src/route.ts lines 108 to 110Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -105,8 +106,5 @@105106 }106107107108 useRouteContext: UseRouteContextRoute<TId> = (opts) => {108- return useMatch({109- from: this.id as any,110- select: (d) => (opts?.select ? opts.select(d.context) : d.context),111- }) as any109+ return useRouteContext({ from: this.id as any, ...(opts as any) }) as any112110 }⚠️ Potential issue | 🟡 Minor
Keep
fromfixed in the bounduseRouteContexthelpers.Spreading
optsafterfromlets a strayfromproperty override the route id at runtime, which breaks the contract of these route-bound APIs and is inconsistent with the other helpers in this file.Suggested fix
- return useRouteContext({ from: this.id, ...(opts as any) }) as any + return useRouteContext({ ...(opts as any), from: this.id }) as anyApply the same ordering to the
RouteApiandRootRoutevariants too.Also applies to: 261-263, 503-505
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/vue-router/src/route.ts` around lines 108 - 110, The bound helper currently spreads opts after setting from, allowing a stray opts.from to override the route id; change the object ordering so opts are spread first and from is set last (e.g., call useRouteContext({ ...(opts as any), from: this.id as any })), and apply the same fix to the RouteApi and RootRoute bound variants referenced elsewhere so their from property cannot be overridden by opts.✅ Addressed in commits e42b833 to f4140b5
Merging this PR will not alter performance#
✅ 6untouched benchmarks
Comparingfix-stale-loader-unrelated(79eec14) withmain(7003ccb)[^unexpected-base]♻️ Duplicate comments (2)
packages/react-router/src/fileRoute.ts (1)
232-234: ⚠️ Potential issue | 🟡 MinorKeep
LazyRoute.useRouteContext()pinned to its route ID.Line 233 spreads
optsafterfrom, so callers can still override the bound route at runtime. Please flip the merge order sothis.options.idalways wins.Suggested fix
useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => { - return useRouteContext({ from: this.options.id, ...(opts as any) }) + return useRouteContext({ ...(opts as any), from: this.options.id }) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-router/src/fileRoute.ts` around lines 232 - 234, The current UseRouteContextRoute implementation spreads caller opts after setting from, so callers can override the pinned route ID; update UseRouteContextRoute (LazyRoute.useRouteContext) to spread opts first and then set from: this.options.id (e.g., call useRouteContext({ ...(opts as any), from: this.options.id })) so the route ID from this.options.id always takes precedence.packages/react-router/src/route.tsx (1)
117-119: ⚠️ Potential issue | 🟡 MinorKeep the bound
useRouteContexthelpers route-scoped.Lines 118, 273, and 545 spread
optsafterfrom, soRouteApi,Route, andRootRoutecallers can override the bound ID and read another route's context. These wrappers should mirror the other bound helpers and makethis.idwin.Suggested fix
useRouteContext: UseRouteContextRoute<TId> = (opts) => { - return useRouteContext({ from: this.id as any, ...(opts as any) }) + return useRouteContext({ ...(opts as any), from: this.id as any }) } useRouteContext: UseRouteContextRoute<TId> = (opts?) => { - return useRouteContext({ from: this.id, ...(opts as any) }) + return useRouteContext({ ...(opts as any), from: this.id }) } useRouteContext: UseRouteContextRoute<RootRouteId> = (opts) => { - return useRouteContext({ from: this.id, ...(opts as any) }) + return useRouteContext({ ...(opts as any), from: this.id }) }Also applies to: 272-274, 544-546
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-router/src/route.tsx` around lines 117 - 119, The bound useRouteContext helper currently spreads opts after setting from, allowing callers to override the bound ID; update the wrappers (the UseRouteContextRoute<TId> implementations used in RouteApi, Route, and RootRoute) to make this.id win by spreading opts first and then setting from: this.id (i.e. return useRouteContext({ ...(opts as any), from: this.id as any })) so the bound route ID cannot be overridden.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Duplicate comments: In `@packages/react-router/src/fileRoute.ts`: - Around line 232-234: The current UseRouteContextRoute implementation spreads caller opts after setting from, so callers can override the pinned route ID; update UseRouteContextRoute (LazyRoute.useRouteContext) to spread opts first and then set from: this.options.id (e.g., call useRouteContext({ ...(opts as any), from: this.options.id })) so the route ID from this.options.id always takes precedence. In `@packages/react-router/src/route.tsx`: - Around line 117-119: The bound useRouteContext helper currently spreads opts after setting from, allowing callers to override the bound ID; update the wrappers (the UseRouteContextRoute<TId> implementations used in RouteApi, Route, and RootRoute) to make this.id win by spreading opts first and then setting from: this.id (i.e. return useRouteContext({ ...(opts as any), from: this.id as any })) so the bound route ID cannot be overridden.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
387f50b4-f462-4c59-91f1-d241aa656000📥 Commits
Reviewing files that changed from the base of the PR and between 68f4530412219830bd07ac664a0d3c8342b14727 and d1f477b0cc0408e2904c887cc53a164ab73a105b.
📒 Files selected for processing (2)
packages/react-router/src/fileRoute.tspackages/react-router/src/route.tsx
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/router-core/src/load-matches.ts (1)
817-831: ⚠️ Potential issue | 🟠 MajorSync
contextbefore returning stale UI during async revalidation.This branch starts
runLoaderin the background, butmatch.contextis left stale until that loader finishes. SincebeforeLoadhas already updated__beforeLoadContext, consumers can observe the previous route context while stale data is being rendered. CallingsyncMatchContext(inner, matchId, index)before launching the async reload keeps context aligned with the new navigation.Proposed fix
} else if (loaderShouldRunAsync && !inner.sync) { + syncMatchContext(inner, matchId, index) loaderIsRunningAsync = true ;(async () => { try { await runLoader(inner, matchPromises, matchId, index, route) const match = inner.router.getMatch(matchId)!🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/router-core/src/load-matches.ts` around lines 817 - 831, When launching the background async loader in the branch guarded by loaderShouldRunAsync && !inner.sync, ensure the match context is synced before returning stale UI: call syncMatchContext(inner, matchId, index) right before starting the async IIFE so match.context and any __beforeLoadContext updates are applied immediately; keep the existing async runLoader(inner, matchPromises, matchId, index, route) invocation and its try/catch/redirect handling unchanged, but remove the window where match.context remains stale by invoking syncMatchContext first.🧹 Nitpick comments (1)
e2e/react-start/spa-mode/tests/app.spec.ts (1)
44-48: Minor: Explicit type annotations are redundant but acceptable.The
{ page: Page }type annotations are technically redundant since Playwright's test fixtures already provide proper typing through@tanstack/router-e2e-utils. However, this doesn't cause any issues and can improve IDE support in some configurations.Also applies to: 64-68
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@e2e/react-start/spa-mode/tests/app.spec.ts` around lines 44 - 48, Remove the redundant explicit TypeScript fixture annotations in the test callbacks — replace the parameter destructuring `async ({ page }: { page: Page }) =>` with `async ({ page }) =>` for the tests named "directly visiting prerendered /posts/1" and the similar test at the second occurrence (the other test using the same `{ page: Page }` annotation); this keeps Playwright's built-in typings via `@tanstack/router-e2e-utils` while avoiding duplicate type declarations and IDE noise.🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/router-core/src/load-matches.ts`: - Around line 807-815: The fallback check comparing prevMatch.id !== match.id never fires because both vars come from router.getMatch(matchId); change the comparison to use the previous match id captured from the prior navigation/state instead of the current store entry. Locate where prevMatch and match are obtained (calls to router.getMatch(matchId)) and replace the identity comparison with something like prevMatchId !== match.id where prevMatchId is read from the previous router state/navigation snapshot (the value that was stored before updating the current matches). Keep the rest of the logic (staleMatchShouldReload, inner.forceStaleReload, match.cause, loaderShouldRunAsync) unchanged so the reload decision correctly detects a changed route instance even when the match entry stays mounted. --- Outside diff comments: In `@packages/router-core/src/load-matches.ts`: - Around line 817-831: When launching the background async loader in the branch guarded by loaderShouldRunAsync && !inner.sync, ensure the match context is synced before returning stale UI: call syncMatchContext(inner, matchId, index) right before starting the async IIFE so match.context and any __beforeLoadContext updates are applied immediately; keep the existing async runLoader(inner, matchPromises, matchId, index, route) invocation and its try/catch/redirect handling unchanged, but remove the window where match.context remains stale by invoking syncMatchContext first. --- Nitpick comments: In `@e2e/react-start/spa-mode/tests/app.spec.ts`: - Around line 44-48: Remove the redundant explicit TypeScript fixture annotations in the test callbacks — replace the parameter destructuring `async ({ page }: { page: Page }) =>` with `async ({ page }) =>` for the tests named "directly visiting prerendered /posts/1" and the similar test at the second occurrence (the other test using the same `{ page: Page }` annotation); this keeps Playwright's built-in typings via `@tanstack/router-e2e-utils` while avoiding duplicate type declarations and IDE noise.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
1774b631-d1d0-4ba0-a7b5-20ba9b64592a📥 Commits
Reviewing files that changed from the base of the PR and between d1f477b0cc0408e2904c887cc53a164ab73a105b and f4140b54d7c431366d1c1b3a4f67cc108e87021c.
📒 Files selected for processing (11)
e2e/react-start/spa-mode/tests/app.spec.tse2e/solid-start/spa-mode/tests/app.spec.tse2e/vue-start/spa-mode/tests/app.spec.tspackages/react-router/src/fileRoute.tspackages/react-router/src/route.tsxpackages/router-core/src/load-matches.tspackages/router-core/src/router.tspackages/solid-router/src/fileRoute.tspackages/solid-router/src/route.tsxpackages/vue-router/src/fileRoute.tspackages/vue-router/src/route.ts
🚧 Files skipped from review as they are similar to previous changes (6)
- packages/solid-router/src/route.tsx
- packages/router-core/src/router.ts
- packages/react-router/src/fileRoute.ts
- packages/solid-router/src/fileRoute.ts
- packages/react-router/src/route.tsx
- packages/vue-router/src/fileRoute.ts
packages/router-core/src/load-matches.ts lines 808 to 817Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -787,6 +804,12 @@787804788805 // If the route is successful and still fresh, just resolve789806 const { status, invalid } = match807+ const staleMatchShouldReload =808+ age > staleAge &&809+ (!!inner.forceStaleReload ||810+ match.cause === 'enter' ||811+ prevMatch.id !== match.id)790812 loaderShouldRunAsync =791- status === 'success' && (invalid || (shouldReload ?? age > staleAge))813+ status === 'success' &&814+ (invalid || (shouldReload ?? staleMatchShouldReload))792815 if (preload && route.options.preload === false) {⚠️ Potential issue | 🟠 Major
The match-identity fallback never fires here.
prevMatchandmatchare both read fromrouter.getMatch(matchId), soprevMatch.id !== match.idcannot becometrue. If this branch is supposed to preserve reloads when the current route instance changes but the route stays mounted (for example via params/loader deps), that signal is lost and the decision collapses tocause === 'enter'/forceStaleReloadonly. Please compare against an id captured from the previous navigation/state instead of the same store entry.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/router-core/src/load-matches.ts` around lines 807 - 815, The fallback check comparing prevMatch.id !== match.id never fires because both vars come from router.getMatch(matchId); change the comparison to use the previous match id captured from the prior navigation/state instead of the current store entry. Locate where prevMatch and match are obtained (calls to router.getMatch(matchId)) and replace the identity comparison with something like prevMatchId !== match.id where prevMatchId is read from the previous router state/navigation snapshot (the value that was stored before updating the current matches). Keep the rest of the logic (staleMatchShouldReload, inner.forceStaleReload, match.cause, loaderShouldRunAsync) unchanged so the reload decision correctly detects a changed route instance even when the match entry stays mounted.✅ Addressed in commits ad7016d to 79eec14
- packages/router-core/src/load-matches.ts lines 836 to 852Resolvedby schiller-manuel(GitHub profile, opens in a new tab)@@ -817,3 +842,9 @@817842 const route = inner.router.looseRoutesById[routeId]!818843819844 if (shouldSkipLoader(inner, matchId)) {845+ const match = inner.router.getMatch(matchId)846+ if (!match) {847+ return inner.matches[index]!848+ }849+850+ syncMatchContext(inner, matchId, index)
Can we avoid this 2nd
syncMatchContextcall if we went through the branch that contains the 1st one? Something likelet hasSynced = false if (...) { } else { syncMatchContext(inner, matchId, index) hasSynced = true } ... if (!hasSynced) syncMatchContext(inner, matchId, index)No change needed here.
- In packages/router-core/src/load-matches.ts, those two syncMatchContext(...) calls are in mutually exclusive branches of the same loadRouteMatch invocation:
- one is inside handleLoader(...) when we decide not to run the loader
- the other is in the top-level if (shouldSkipLoader(...)) branch
- A single call cannot hit both, because handleLoader(...) only runs in the else branch.
- In packages/router-core/src/load-matches.ts, those two syncMatchContext(...) calls are in mutually exclusive branches of the same loadRouteMatch invocation:
- packages/router-core/src/load-matches.ts line 853OutdatedResolvedby schiller-manuel(GitHub profile, opens in a new tab)@@ -817,5 +842,12 @@817842 const route = inner.router.looseRoutesById[routeId]!818843819844 if (shouldSkipLoader(inner, matchId)) {845+ const match = inner.router.getMatch(matchId)846+ if (!match) {847+ return inner.matches[index]!848+ }849+850+ syncMatchContext(inner, matchId, index)851+820852 if (isServer ?? inner.router.isServer) {821- return inner.router.getMatch(matchId)!853+ return match
Are you sure we can just
return matchhere? With the current implementation, every time we callupdateMatch()thematchbecomes a new object, so any previously heldmatchwill not contain up-to-date data.reverted this, good catch
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
3ccefb00-0709-4c95-902f-4aa29ea68cd1📥 Commits
Reviewing files that changed from the base of the PR and between f4140b54d7c431366d1c1b3a4f67cc108e87021c and 79eec144aa0efe473de0951bf962e1d4f01e8c77.
📒 Files selected for processing (2)
packages/router-core/src/load-matches.tspackages/router-core/tests/load.test.ts📝 Walkthrough
Walkthrough#
Adds a forceStaleReload option to the load flow to enable explicit revalidation of stale matches, switches several framework route helpers to delegate to useRouteContext, updates docs about staleTime semantics, adjusts unit/e2e test expectations, and adds tests for stale-reload triggers.
Changes#
docs/router/api/router/RouterType.md,docs/router/guide/data-loading.mdrouter.load(); noterouter.invalidate()forces reload of active matches.packages/router-core/src/load-matches.ts,packages/router-core/src/router.tsforceStaleReload?: booleanto public/internal load APIs; introducesyncMatchContext, propagate previousRouteMatchId, update stale-reload decision logic and call sites; passforceStaleReloadfrom router when loading same-location navigations.packages/react-router/src/fileRoute.ts,packages/react-router/src/route.tsx,packages/solid-router/src/fileRoute.ts,packages/solid-router/src/route.tsx,packages/vue-router/src/fileRoute.ts,packages/vue-router/src/route.tsuseMatch-based implementations with directuseRouteContext({... from: this.id, ...opts})delegation for LazyRoute/Route/RouteApi/RootRoute helpers. Public APIs unchanged.packages/router-core/tests/load.test.tsrouter.load()revalidation (duplicate suite appears in diff).packages/react-router/tests/store-updates-during-navigation.test.tsx,packages/solid-router/tests/store-updates-during-navigation.test.tsx,packages/vue-router/tests/store-updates-during-navigation.test.tsxe2e/react-start/spa-mode/tests/app.spec.ts,e2e/solid-start/spa-mode/tests/app.spec.ts,e2e/vue-start/spa-mode/tests/app.spec.ts{ loader, context }shape; split assertions into separate loader and context validation passes and update expected values for prerendered vs client navigation scenarios.Sequence Diagram(s)#
Estimated code review effort#
🎯 4 (Complex) | ⏱️ ~60 minutes
Possibly related PRs#
Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (2 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands and usage tips.