|
1 | 1 | import * as Vue from 'vue' |
2 | 2 | import type { AnyRouter } from '@tanstack/router-core' |
3 | 3 |
|
4 | | -// Create a router context symbol |
5 | | -export const RouterSymbol = Symbol( |
| 4 | +export const routerContext = Symbol( |
6 | 5 | 'TanStackRouter', |
7 | 6 | ) as Vue.InjectionKey<AnyRouter> |
8 | 7 |
|
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 | | - |
35 | 8 | /** |
36 | 9 | * Provides the router to all child components |
37 | 10 | */ |
38 | 11 | export function provideRouter(router: AnyRouter): void { |
39 | | - Vue.provide(getRouterContext(), router) |
| 12 | + Vue.provide(routerContext, router) |
40 | 13 | } |
41 | 14 |
|
42 | 15 | /** |
43 | 16 | * Injects the router from the component tree |
44 | 17 | */ |
45 | 18 | export function injectRouter(): AnyRouter { |
46 | | - const router = Vue.inject<AnyRouter | null>(getRouterContext(), null) |
| 19 | + const router = Vue.inject<AnyRouter | null>(routerContext, null) |
47 | 20 | if (!router) { |
48 | 21 | throw new Error( |
49 | 22 | 'No TanStack Router found in component tree. Did you forget to add a RouterProvider component?', |
|
0 commit comments