Skip to content

Commit 59e0ef3

Browse files
feat(client): expose SSG context globally
- Add __VITE_REACT_SSG_CONTEXT__ to window object - Store SSG context in global scope for client-side access - Update all router implementations (default, single-page, tanstack) Signed-off-by: Riri <Daydreamerriri@outlook.com>
1 parent b6a900f commit 59e0ef3

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/client/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ export function ViteReactSSG(
109109
)
110110
}
111111

112-
const { router } = await createRoot(true)
112+
const context = await createRoot(true)
113+
window.__VITE_REACT_SSG_CONTEXT__ = context
114+
115+
const { router } = context
113116
const app = (
114117
<HelmetProvider>
115118
<RouterProvider router={router!} future={{ v7_startTransition }} />
@@ -168,6 +171,7 @@ declare global {
168171
interface Window {
169172
__VITE_REACT_SSG_STATIC_LOADER_DATA__: any
170173
__VITE_REACT_SSG_HASH__: string
174+
__VITE_REACT_SSG_CONTEXT__: ViteReactSSGContext<true>
171175
}
172176
}
173177

src/client/single-page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export function ViteReactSSG(
8282
return
8383
}
8484

85-
await createRoot(true)
85+
const context = await createRoot(true)
86+
window.__VITE_REACT_SSG_CONTEXT__ = context as any
8687
const app = (
8788
<HelmetProvider>
8889
{App}

src/client/tanstack.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,10 @@ export function Experimental_ViteReactSSG(
169169
return
170170
}
171171

172-
const { router } = await createRoot(true)
172+
const context = await createRoot(true)
173+
window.__VITE_REACT_SSG_CONTEXT__ = context as any
174+
175+
const { router } = context
173176
const isSSR = document.querySelector('[data-server-rendered=true]') !== null
174177
if (!isSSR && process.env.NODE_ENV === 'development') {
175178
render(
@@ -199,6 +202,8 @@ declare global {
199202
interface Window {
200203
__VITE_REACT_SSG_STATIC_LOADER_DATA__: any
201204
__VITE_REACT_SSG_HASH__: string
205+
// @ts-expect-error global variable
206+
__VITE_REACT_SSG_CONTEXT__: ViteReactSSGContext<true>
202207
}
203208
}
204209

0 commit comments

Comments
 (0)