fix: remove router context caching#6734
Conversation
📝 WalkthroughWalkthroughThis 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
|
View your CI Pipeline Execution ↗ for commit 96e07c5
☁️ Nx Cloud last updated this comment at |
Bundle Size Benchmarks
Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better. |
There was a problem hiding this comment.
🧹 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 anycast is required becauserouterContextis typed asInjectionKey<AnyRouter>while the function returns a genericTRouter. 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 ofnull as unknown as AnyRouter.Line 4–5 uses a double assertion that sidesteps strict typing. Consider
AnyRouter | undefined(ornull) and let consumers handle missing providers; this also enables removing theas anyinpackages/solid-router/src/useRouter.tsxLine 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.
Summary by CodeRabbit
Breaking Changes
Improvements