-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry-server.tsx
54 lines (52 loc) · 1.33 KB
/
entry-server.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
50
51
52
53
54
import type { IncomingMessage, ServerResponse } from "node:http";
import ReactDOMServer from "react-dom/server";
import { App } from "./app";
export default async function handler(
_req: IncomingMessage,
res: ServerResponse,
) {
const htmlStream = ReactDOMServer.renderToPipeableStream(<Root />, {
bootstrapModules: ["/src/check-preamble.js", "/src/entry-client"],
onShellReady() {
res.setHeader("content-type", "text/html");
htmlStream.pipe(res);
},
onShellError() {
res.statusCode = 500;
res.setHeader("content-type", "text/html");
res.end("<h1>Something went wrong</h1>");
},
});
}
function Root() {
return (
<html>
<head>
<meta charSet="utf-8" />
<title>test</title>
{import.meta.env.DEV && (
<>
<script type="module" src="/@vite/client" />
<script
// `async` doesn't seem to affect the result of `check-preamble.js`
async
type="module"
dangerouslySetInnerHTML={{
__html: `
import RefreshRuntime from '/@react-refresh'
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
`,
}}
></script>
</>
)}
</head>
<body>
<App />
</body>
</html>
);
}