-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathprerender.js
28 lines (21 loc) · 866 Bytes
/
prerender.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
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'url'
import { render } from './dist/server/entry-server.js'
const toAbsolute = (p) =>
path.resolve(path.dirname(fileURLToPath(import.meta.url)), p)
const manifest = JSON.parse(
fs.readFileSync(toAbsolute('dist/ssr-manifest.json'), 'utf-8')
)
const template = fs.readFileSync(toAbsolute('dist/index.html'), 'utf-8')
// pre-render each route...
const [appHtml, preloadLinks] = await render(manifest)
const html = template
.replace('<!--preload-links-->', preloadLinks)
.replace('<!--app-html-->', appHtml)
const filePath = 'dist/index.html'
fs.writeFileSync(toAbsolute(filePath), html)
console.log('pre-rendered:', filePath)
// done, delete ssr manifest
fs.rmSync(toAbsolute('dist/ssr-manifest.json'))
fs.rmSync(toAbsolute('dist/server'), { recursive: true })