-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhydrate.tsx
49 lines (46 loc) · 1.35 KB
/
hydrate.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { hydrateRoot, type ErrorInfo } from "react-dom/client";
import { RouterHost } from "./router";
import { getRouteMatcher } from "./router/utils/get-route-matcher";
import type { ServerSideProps } from "./types";
const globalX = globalThis as unknown as {
__PAGES_DIR__: string;
__INITIAL_ROUTE__: string;
__ROUTES__: Record<string, string>;
__STATIC_PROPS__?: Record<string, unknown>;
__SERVERSIDE_PROPS__?: any;
};
const match = getRouteMatcher(globalX.__ROUTES__);
export async function hydrate(
Shell: React.ComponentType<
{ children: React.ReactElement } & ServerSideProps
>,
{
onRecoverableError = () => void 8,
...options
}: Omit<
React.ComponentPropsWithoutRef<typeof RouterHost>,
"Shell" | "children"
> & {
onRecoverableError?: (error: unknown, errorInfo: ErrorInfo) => void;
} = {}
) {
const matched = match(globalX.__INITIAL_ROUTE__.split("?")[0])!;
const Initial = await import(matched.value);
return hydrateRoot(
document,
<RouterHost
Shell={Shell}
staticProps={globalX.__STATIC_PROPS__}
{...options}
>
<Shell
route={globalX.__INITIAL_ROUTE__}
{...globalX.__STATIC_PROPS__}
{...globalX.__SERVERSIDE_PROPS__}
>
<Initial.default {...globalX.__SERVERSIDE_PROPS__?.props} />
</Shell>
</RouterHost>,
{ onRecoverableError }
);
}