-
-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathentry.rsc.tsx
More file actions
62 lines (52 loc) · 1.72 KB
/
entry.rsc.tsx
File metadata and controls
62 lines (52 loc) · 1.72 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { renderToReadableStream } from '@vitejs/plugin-rsc/rsc'
import { Root, getStaticPaths } from '../root'
import { RSC_POSTFIX, type RscPayload } from './shared'
export { getStaticPaths }
export default async function handler(request: Request): Promise<Response> {
let url = new URL(request.url)
let isRscRequest = false
if (url.pathname.endsWith(RSC_POSTFIX)) {
isRscRequest = true
url.pathname = url.pathname.slice(0, -RSC_POSTFIX.length)
}
const rscPayload: RscPayload = { root: <Root url={url} /> }
const rscStream = renderToReadableStream<RscPayload>(rscPayload)
if (isRscRequest) {
return new Response(rscStream, {
headers: {
'content-type': 'text/x-component;charset=utf-8',
vary: 'accept',
},
})
}
const ssr = await import.meta.viteRsc.loadModule<
typeof import('./entry.ssr')
>('ssr', 'index')
const htmlStream = await ssr.renderHtml(rscStream)
return new Response(htmlStream, {
headers: {
'content-type': 'text/html;charset=utf-8',
vary: 'accept',
},
})
}
// return both rsc and html streams at once for ssg
export async function handleSsg(request: Request): Promise<{
html: ReadableStream<Uint8Array>
rsc: ReadableStream<Uint8Array>
}> {
const url = new URL(request.url)
const rscPayload: RscPayload = { root: <Root url={url} /> }
const rscStream = renderToReadableStream<RscPayload>(rscPayload)
const [rscStream1, rscStream2] = rscStream.tee()
const ssr = await import.meta.viteRsc.loadModule<
typeof import('./entry.ssr')
>('ssr', 'index')
const htmlStream = await ssr.renderHtml(rscStream1, {
ssg: true,
})
return { html: htmlStream, rsc: rscStream2 }
}
if (import.meta.hot) {
import.meta.hot.accept()
}