Skip to content

Commit 52b7d31

Browse files
committed
add static props
1 parent dda2918 commit 52b7d31

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

example/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Bun.serve({
1313
"x-powered-by": "bun",
1414
"cache-control": "max-age=14400, immutable",
1515
},
16+
staticProps: {
17+
meta: require("./.build/.meta.json").hashed,
18+
},
1619
});
1720
if (response) return response;
1821
return new Response("Not found", {

example/shell.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
export const ExampleShell: React.FC<{ children: React.ReactElement }> = ({
2-
children,
3-
}) => (
1+
export const ExampleShell: React.FC<{
2+
children: React.ReactElement;
3+
meta?: Record<string, string>;
4+
}> = ({ children, meta }) => (
45
<html>
56
<head>
67
<title>Example</title>
78
</head>
8-
<body>{children}</body>
9+
<body>
10+
<div>{children}</div>
11+
<output>{JSON.stringify(meta)}</output>
12+
</body>
913
</html>
1014
);

hydrate.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const globalX = globalThis as unknown as {
77
__PAGES_DIR__: string;
88
__INITIAL_ROUTE__: string;
99
__ROUTES__: Record<string, string>;
10+
__STATIC_PROPS__?: Record<string, unknown>;
1011
__SERVERSIDE_PROPS__?: any;
1112
};
1213

@@ -33,6 +34,7 @@ export async function hydrate(
3334
<RouterHost Shell={Shell} {...options}>
3435
<Shell
3536
route={globalX.__INITIAL_ROUTE__}
37+
{...globalX.__STATIC_PROPS__}
3638
{...globalX.__SERVERSIDE_PROPS__}
3739
>
3840
<Initial.default {...globalX.__SERVERSIDE_PROPS__?.props} />

index.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class StaticRouters {
5050
},
5151
noStreaming,
5252
staticHeaders,
53+
staticProps,
5354
}: {
5455
Shell: React.ComponentType<{ children: React.ReactElement }>;
5556
preloadScript?: string;
@@ -58,6 +59,7 @@ export class StaticRouters {
5859
onError?(error: unknown, errorInfo: React.ErrorInfo): string | void;
5960
noStreaming?: boolean;
6061
staticHeaders?: HeadersInit;
62+
staticProps?: Record<string, unknown>;
6163
}
6264
): Promise<Response | null> {
6365
const { pathname, search } = new URL(request.url);
@@ -98,7 +100,7 @@ export class StaticRouters {
98100
});
99101
}
100102
const stream = await renderToReadableStream(
101-
<Shell route={serverSide.pathname + search} {...result}>
103+
<Shell route={serverSide.pathname + search} {...staticProps} {...result}>
102104
<module.default {...result?.props} />
103105
</Shell>,
104106
{
@@ -108,6 +110,7 @@ export class StaticRouters {
108110
`__PAGES_DIR__=${JSON.stringify(this.pageDir)}`,
109111
`__INITIAL_ROUTE__=${JSON.stringify(serverSide.pathname + search)}`,
110112
`__ROUTES__=${this.#routes_dump}`,
113+
!!staticProps && `__STATIC_PROPS__=${NJSON.stringify(staticProps)}`,
111114
`__SERVERSIDE_PROPS__=${stringified}`,
112115
]
113116
.filter(Boolean)

0 commit comments

Comments
 (0)