-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.ts
38 lines (30 loc) · 1.23 KB
/
prerender.ts
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
// Pre-render the app into static HTML.
// run `yarn generate` and then `dist/static` can be served as a static site.
const fs = require('fs')
const path = require('path')
const toAbsolute = (p) => path.resolve(__dirname, p)
const manifest = require('./dist/static/ssr-manifest.json')
const template = fs.readFileSync(toAbsolute('dist/static/index.html'), 'utf-8')
// @ts-ignore
import { render } from './dist/server/entry-server'
// determine routes to pre-render from src/pages
const routesToPrerender = fs
.readdirSync(toAbsolute('src/pages'))
.map((file) => {
const name = file.replace(/\.vue$/, '').toLowerCase()
return name === 'home' ? `/` : `/${name}`
})
;(async () => {
// pre-render each route...
for (const url of routesToPrerender) {
const [appHtml, preloadLinks] = await render(url, manifest)
const html = template
.replace(`<!--preload-links-->`, preloadLinks)
.replace(`<!--app-html-->`, appHtml)
const filePath = `dist/static${url === '/' ? '/index' : url}.html`
fs.writeFileSync(toAbsolute(filePath), html)
console.log('pre-rendered:', filePath)
}
// done, delete ssr manifest
fs.unlinkSync(toAbsolute('dist/static/ssr-manifest.json'))
})()