Skip to content

fix: remove router context caching#6734

Merged
schiller-manuel merged 1 commit intomainfrom
fix-no-router-context-caching
Feb 22, 2026
Merged

fix: remove router context caching#6734
schiller-manuel merged 1 commit intomainfrom
fix-no-router-context-caching

Conversation

@schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel commented Feb 22, 2026

Summary by CodeRabbit

  • Breaking Changes

    • Removed a context accessor from the public API across React, Solid, and Vue router packages.
  • Improvements

    • Simplified context provisioning by removing global caching mechanisms and streamlining how router context is accessed internally.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 22, 2026

📝 Walkthrough

Walkthrough

This PR refactors how router context is accessed across react-router, solid-router, and vue-router packages. The getRouterContext() function is removed and replaced with direct exports of routerContext (a context object or Symbol), simplifying context provisioning and eliminating previous SSR caching logic.

Changes

Cohort / File(s) Summary
RouterProvider Updates
packages/react-router/src/RouterProvider.tsx, packages/solid-router/src/RouterProvider.tsx
Updated to import and use routerContext directly instead of calling getRouterContext().
Public API Cleanup
packages/react-router/src/index.tsx, packages/solid-router/src/index.tsx, packages/vue-router/src/index.tsx
Removed getRouterContext from public exports across all three packages.
Context Refactoring
packages/react-router/src/routerContext.tsx, packages/solid-router/src/routerContext.tsx
Changed routerContext from private to exported constant and removed getRouterContext() function along with global window augmentation logic.
Context Refactoring (Vue)
packages/vue-router/src/routerContext.tsx
Renamed RouterSymbol to routerContext, removed getRouterContext() function and SSR caching logic, updated provideRouter and injectRouter references.
Hook Updates
packages/react-router/src/useRouter.tsx, packages/solid-router/src/useRouter.tsx, packages/vue-router/src/useRouter.tsx
Updated useRouter/injectRouter to use routerContext directly with appropriate context consumption methods (React.useContext, Solid.useContext, Vue.inject).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

package: react-router, package: solid-router, package: vue-router

Suggested reviewers

  • brenelz
  • birkskyum

Poem

🐰 A rabbit hops through context code,
No more getters on the road!
Direct exports clean and bright,
Three packages aligned just right! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: remove router context caching' accurately and concisely summarizes the main change across all packages, which involves removing the getRouterContext() function and its global window-based caching mechanism.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-no-router-context-caching

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Feb 22, 2026

View your CI Pipeline Execution ↗ for commit 96e07c5

Command Status Duration Result
nx run tanstack-router-e2e-bundle-size:build --... ✅ Succeeded 1m 27s View ↗

☁️ Nx Cloud last updated this comment at 2026-02-22 22:46:12 UTC

@github-actions
Copy link
Contributor

Bundle Size Benchmarks

  • Commit: 5c65363fddb3
  • Measured at: 2026-02-22T22:30:43.207Z
  • Baseline source: history:53eb52abda11
  • Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Raw Brotli Trend
react-router.minimal 86.72 KiB -61 B (-0.07%) 272.45 KiB 75.31 KiB ▅▅▅▅▅██▁
react-router.full 89.79 KiB -50 B (-0.05%) 282.92 KiB 78.10 KiB █████▇▇▁
solid-router.minimal 35.99 KiB -41 B (-0.11%) 107.61 KiB 32.34 KiB █████▆▆▁
solid-router.full 40.34 KiB -40 B (-0.10%) 120.70 KiB 36.25 KiB █████▆▆▁
vue-router.minimal 51.83 KiB -45 B (-0.08%) 147.61 KiB 46.66 KiB █████▅▅▁
vue-router.full 56.69 KiB -40 B (-0.07%) 163.21 KiB 50.87 KiB █████▅▅▁
react-start.minimal 99.22 KiB -32 B (-0.03%) 311.44 KiB 85.84 KiB ▇▇▇▇▇██▁
react-start.full 102.59 KiB -60 B (-0.06%) 321.36 KiB 88.70 KiB ███████▁
solid-start.minimal 48.23 KiB -47 B (-0.10%) 145.04 KiB 42.66 KiB █████▇▇▁
solid-start.full 53.71 KiB -38 B (-0.07%) 160.99 KiB 47.35 KiB █████▇▇▁

Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 22, 2026

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6734

@tanstack/eslint-plugin-router

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

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6734

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6734

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6734

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6734

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6734

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6734

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6734

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6734

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6734

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6734

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6734

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6734

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6734

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6734

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6734

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6734

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6734

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6734

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6734

commit: 96e07c5

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
packages/vue-router/src/useRouter.tsx (1)

9-9: Type cast is necessary for generic support but reduces type safety.

The as any cast is required because routerContext is typed as InjectionKey<AnyRouter> while the function returns a generic TRouter. This is a common Vue pattern limitation when combining generics with injection keys.

Consider if a more type-safe approach is feasible in the future, such as having the consumer provide the expected type via a factory function. However, this is acceptable for now given the existing pattern.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/vue-router/src/useRouter.tsx` at line 9, The current use of "as any"
in the Vue.inject call reduces type safety when retrieving the injected router
(see useRouter and routerContext); leave the cast to preserve generic support
but replace the bare cast with an explicit explanatory TODO comment above the
injection line explaining why the cast is required and suggesting a future, more
type-safe approach (e.g., accepting an InjectionKey<TRouter> or a factory that
returns TRouter), so the code keeps current behavior while documenting the
tradeoff for future refactors.
packages/solid-router/src/routerContext.tsx (1)

4-5: Prefer a nullable context type instead of null as unknown as AnyRouter.

Line 4–5 uses a double assertion that sidesteps strict typing. Consider AnyRouter | undefined (or null) and let consumers handle missing providers; this also enables removing the as any in packages/solid-router/src/useRouter.tsx Line 9.

♻️ Proposed adjustment
-export const routerContext = Solid.createContext<AnyRouter>(
-  null as unknown as AnyRouter,
-)
+export const routerContext = Solid.createContext<AnyRouter | undefined>(
+  undefined,
+)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/solid-router/src/routerContext.tsx` around lines 4 - 5, Change
routerContext to use a nullable type (e.g., Solid.createContext<AnyRouter |
undefined> or AnyRouter | null) instead of the double-assertion null as unknown
as AnyRouter; update the context creation in routerContext (symbol:
routerContext) to that nullable type and adjust consumers (notably the useRouter
hook in useRouter.tsx) to handle the undefined/null provider so you can remove
the `as any`/assertion in useRouter.tsx.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/solid-router/src/routerContext.tsx`:
- Around line 4-5: Change routerContext to use a nullable type (e.g.,
Solid.createContext<AnyRouter | undefined> or AnyRouter | null) instead of the
double-assertion null as unknown as AnyRouter; update the context creation in
routerContext (symbol: routerContext) to that nullable type and adjust consumers
(notably the useRouter hook in useRouter.tsx) to handle the undefined/null
provider so you can remove the `as any`/assertion in useRouter.tsx.

In `@packages/vue-router/src/useRouter.tsx`:
- Line 9: The current use of "as any" in the Vue.inject call reduces type safety
when retrieving the injected router (see useRouter and routerContext); leave the
cast to preserve generic support but replace the bare cast with an explicit
explanatory TODO comment above the injection line explaining why the cast is
required and suggesting a future, more type-safe approach (e.g., accepting an
InjectionKey<TRouter> or a factory that returns TRouter), so the code keeps
current behavior while documenting the tradeoff for future refactors.

@schiller-manuel schiller-manuel merged commit eeb8c55 into main Feb 22, 2026
8 checks passed
@schiller-manuel schiller-manuel deleted the fix-no-router-context-caching branch February 22, 2026 22:48
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.

1 participant