-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathentry.ssr.tsx
More file actions
43 lines (39 loc) · 1.54 KB
/
entry.ssr.tsx
File metadata and controls
43 lines (39 loc) · 1.54 KB
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
import { createFromReadableStream } from '@vitejs/plugin-rsc/ssr'
import { renderToReadableStream as renderHTMLToReadableStream } from 'react-dom/server.edge'
import {
unstable_routeRSCServerRequest as routeRSCServerRequest,
unstable_RSCStaticRouter as RSCStaticRouter,
} from 'react-router'
// pass serializable values (via turbo-stream) to ssr environment.
// passing entire `request` and `fetchServer` are not necessary since `routeRSCServerRequest` works like this
// https://github.com/remix-run/react-router/blob/20d8307d4a51c219f6e13e0b66461e7162d944e4/packages/react-router/lib/rsc/server.ssr.tsx#L95-L102
export async function generateHTML(
url: string,
headers: Headers,
rscResponse: Response,
): Promise<Response> {
return await routeRSCServerRequest({
// The incoming request.
request: new Request(url, { headers }),
// How to call the React Server.
fetchServer: async () => rscResponse,
// Provide the React Server touchpoints.
createFromReadableStream,
// Render the router to HTML.
async renderHTML(getPayload) {
const payload = await getPayload()
const formState =
payload.type === 'render' ? await payload.formState : undefined
const bootstrapScriptContent =
await import.meta.viteRsc.loadBootstrapScriptContent('index')
return await renderHTMLToReadableStream(
<RSCStaticRouter getPayload={getPayload} />,
{
bootstrapScriptContent,
// @ts-expect-error - no types for this yet
formState,
},
)
},
})
}