Skip to content

Commit eeb8c55

Browse files
fix: remove router context caching (#6734)
1 parent 951134a commit eeb8c55

File tree

11 files changed

+13
-96
lines changed

11 files changed

+13
-96
lines changed

packages/react-router/src/RouterProvider.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import { Matches } from './Matches'
3-
import { getRouterContext } from './routerContext'
3+
import { routerContext } from './routerContext'
44
import type {
55
AnyRouter,
66
RegisteredRouter,
@@ -33,8 +33,6 @@ export function RouterContextProvider<
3333
})
3434
}
3535

36-
const routerContext = getRouterContext()
37-
3836
const provider = (
3937
<routerContext.Provider value={router as AnyRouter}>
4038
{children}

packages/react-router/src/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,6 @@ export { useNavigate, Navigate } from './useNavigate'
298298
export { useParams } from './useParams'
299299
export { useSearch } from './useSearch'
300300

301-
export {
302-
getRouterContext, // SSR
303-
} from './routerContext'
304-
305301
export { useRouteContext } from './useRouteContext'
306302
export { useRouter } from './useRouter'
307303
export { useRouterState } from './useRouterState'
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,4 @@
11
import * as React from 'react'
22
import type { AnyRouter } from '@tanstack/router-core'
33

4-
declare global {
5-
interface Window {
6-
__TSR_ROUTER_CONTEXT__?: React.Context<AnyRouter>
7-
}
8-
}
9-
10-
const routerContext = React.createContext<AnyRouter>(null!)
11-
12-
export function getRouterContext() {
13-
if (typeof document === 'undefined') {
14-
return routerContext
15-
}
16-
17-
if (window.__TSR_ROUTER_CONTEXT__) {
18-
return window.__TSR_ROUTER_CONTEXT__
19-
}
20-
21-
window.__TSR_ROUTER_CONTEXT__ = routerContext as any
22-
23-
return routerContext
24-
}
4+
export const routerContext = React.createContext<AnyRouter>(null!)

packages/react-router/src/useRouter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import warning from 'tiny-warning'
3-
import { getRouterContext } from './routerContext'
3+
import { routerContext } from './routerContext'
44
import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
55

66
/**
@@ -16,7 +16,7 @@ import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
1616
export function useRouter<TRouter extends AnyRouter = RegisteredRouter>(opts?: {
1717
warn?: boolean
1818
}): TRouter {
19-
const value = React.useContext(getRouterContext())
19+
const value = React.useContext(routerContext)
2020
warning(
2121
!((opts?.warn ?? true) && !value),
2222
'useRouter must be used inside a <RouterProvider> component!',

packages/solid-router/src/RouterProvider.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getRouterContext } from './routerContext'
1+
import { routerContext } from './routerContext'
22
import { SafeFragment } from './SafeFragment'
33
import { Matches } from './Matches'
44
import type {
@@ -28,8 +28,6 @@ export function RouterContextProvider<
2828
},
2929
})
3030

31-
const routerContext = getRouterContext()
32-
3331
const OptionalWrapper = router.options.Wrap || SafeFragment
3432

3533
return (

packages/solid-router/src/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,6 @@ export { useNavigate, Navigate } from './useNavigate'
304304
export { useParams } from './useParams'
305305
export { useSearch } from './useSearch'
306306

307-
export {
308-
getRouterContext, // SSR
309-
} from './routerContext'
310-
311307
export { useRouteContext } from './useRouteContext'
312308
export { useRouter } from './useRouter'
313309
export { useRouterState } from './useRouterState'
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
import * as Solid from 'solid-js'
22
import type { AnyRouter } from '@tanstack/router-core'
33

4-
declare global {
5-
interface Window {
6-
__TSR_ROUTER_CONTEXT__?: Solid.Context<AnyRouter>
7-
}
8-
}
9-
10-
const routerContext = Solid.createContext<AnyRouter>(
4+
export const routerContext = Solid.createContext<AnyRouter>(
115
null as unknown as AnyRouter,
126
)
13-
14-
export function getRouterContext() {
15-
if (typeof document === 'undefined') {
16-
return routerContext
17-
}
18-
19-
if (window.__TSR_ROUTER_CONTEXT__) {
20-
return window.__TSR_ROUTER_CONTEXT__
21-
}
22-
23-
window.__TSR_ROUTER_CONTEXT__ = routerContext as any
24-
25-
return routerContext
26-
}

packages/solid-router/src/useRouter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as Solid from 'solid-js'
22
import warning from 'tiny-warning'
3-
import { getRouterContext } from './routerContext'
3+
import { routerContext } from './routerContext'
44
import type { AnyRouter, RegisteredRouter } from '@tanstack/router-core'
55

66
export function useRouter<TRouter extends AnyRouter = RegisteredRouter>(opts?: {
77
warn?: boolean
88
}): TRouter {
9-
const value = Solid.useContext(getRouterContext() as any)
9+
const value = Solid.useContext(routerContext as any)
1010
warning(
1111
!((opts?.warn ?? true) && !value),
1212
'useRouter must be used inside a <RouterProvider> component!',

packages/vue-router/src/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,6 @@ export { useNavigate, Navigate } from './useNavigate'
298298
export { useParams } from './useParams'
299299
export { useSearch } from './useSearch'
300300

301-
export {
302-
getRouterContext, // SSR
303-
} from './routerContext'
304-
305301
export { useRouteContext } from './useRouteContext'
306302
export { useRouter } from './useRouter'
307303
export { useRouterState } from './useRouterState'

packages/vue-router/src/routerContext.tsx

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,22 @@
11
import * as Vue from 'vue'
22
import type { AnyRouter } from '@tanstack/router-core'
33

4-
// Create a router context symbol
5-
export const RouterSymbol = Symbol(
4+
export const routerContext = Symbol(
65
'TanStackRouter',
76
) as Vue.InjectionKey<AnyRouter>
87

9-
declare global {
10-
interface Window {
11-
__TSR_ROUTER_CONTEXT__?: Vue.InjectionKey<AnyRouter>
12-
}
13-
}
14-
15-
/**
16-
* Gets the router context, handling server-side rendering
17-
* and ensuring a single instance across the application
18-
*/
19-
export function getRouterContext(): Vue.InjectionKey<AnyRouter> {
20-
if (typeof document === 'undefined') {
21-
// For SSR, return the symbol directly
22-
return RouterSymbol
23-
}
24-
25-
// In the browser, check if we have a cached context
26-
if (window.__TSR_ROUTER_CONTEXT__) {
27-
return window.__TSR_ROUTER_CONTEXT__
28-
}
29-
30-
// Create and cache the context
31-
window.__TSR_ROUTER_CONTEXT__ = RouterSymbol
32-
return RouterSymbol
33-
}
34-
358
/**
369
* Provides the router to all child components
3710
*/
3811
export function provideRouter(router: AnyRouter): void {
39-
Vue.provide(getRouterContext(), router)
12+
Vue.provide(routerContext, router)
4013
}
4114

4215
/**
4316
* Injects the router from the component tree
4417
*/
4518
export function injectRouter(): AnyRouter {
46-
const router = Vue.inject<AnyRouter | null>(getRouterContext(), null)
19+
const router = Vue.inject<AnyRouter | null>(routerContext, null)
4720
if (!router) {
4821
throw new Error(
4922
'No TanStack Router found in component tree. Did you forget to add a RouterProvider component?',

0 commit comments

Comments
 (0)