-
Notifications
You must be signed in to change notification settings - Fork 4
/
ssr-dev-server.js
55 lines (47 loc) · 1.78 KB
/
ssr-dev-server.js
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
import fs from 'fs'
import { createServer } from 'http'
import { createServer as createViteDevServer } from 'vite'
const PORT = Number(process.env.PORT) || 5173
const vite = await createViteDevServer({
server: { middlewareMode: true },
appType: 'custom'
})
createServer((req, res) => {
vite.middlewares(req, res, async () => {
try {
// 1. Read index.html
let template = fs.readFileSync('index.html', 'utf-8')
// 2. Apply Vite HTML transforms. This injects the Vite HMR client,
// and also applies HTML transforms from Vite plugins, e.g. global
// preambles from @vitejs/plugin-react
template = await vite.transformIndexHtml(req.url, template)
// 3. Load the server entry. ssrLoadModule automatically transforms
// ESM source code to be usable in Node.js! There is no bundling
// required, and provides efficient invalidation similar to HMR.
const { default: render } = await vite.ssrLoadModule('/src/render.ts')
// 4. render the app HTML. This assumes entry-server.js's exported
// `render` function calls appropriate framework SSR APIs,
// e.g. ReactDOMServer.renderToString()
const {
statusCode = 200,
statusMessage,
headers,
body
} = await render({
url: req.url,
template,
headers: req.headers
})
res.writeHead(statusCode, statusMessage, headers)
res.end(body)
} catch (e) {
// If an error is caught, let Vite fix the stack trace so it maps back
// to your actual source code.
vite.ssrFixStacktrace(e)
console.error(e)
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end(e.message)
}
})
}).listen(PORT)
console.log(`Server running at http://localhost:${PORT}`)