forked from surmon-china/surmon-china.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.js
79 lines (70 loc) · 2.49 KB
/
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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs')
const path = require('path')
const vite = require('vite')
const INDEX_ROUTE = '/'
const toAbsolute = (_path) => path.resolve(__dirname, _path)
const pageRoutes = fs.readdirSync(toAbsolute('src/pages')).map((file) => {
const name = file.replace(/\.vue$/, '').toLowerCase()
return name === 'home' ? INDEX_ROUTE : `/${name}`
})
const renderRedirectionHTML = (url) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0; url='https://github.surmon.me${url}'" />
</head>
</html>
`
;(async () => {
try {
// empty dist dir
fs.rmSync(toAbsolute('dist'), { recursive: true, force: true })
console.log('Bundle CSR...\n')
await vite.build({ build: { emptyOutDir: true } })
console.log('Bundle SSR...\n')
await vite.build({
ssr: { noExternal: ['swiper'] },
build: {
ssr: true,
minify: true,
manifest: false,
emptyOutDir: false,
outDir: toAbsolute('dist/ssr'),
rollupOptions: {
input: toAbsolute('src/ssr.ts')
}
}
})
// MARK: after CSR bundled & before pre-render routes
const template = fs.readFileSync(toAbsolute('dist/index.html'), 'utf-8')
const { render } = require('./dist/ssr/ssr.js')
// pre-render each route...
for (const url of pageRoutes) {
const { appHTML, metas } = await render(url)
const html = template
.replace(/<title>[\s\S]*<\/title>/, '')
.replace(`<html`, () => `<html ${metas.htmlAttrs} `)
.replace(`<head>`, () => `<head>\n${metas.headTags}`)
.replace(`<body>`, () => `<body ${metas.bodyAttrs}>`)
.replace(`<!--app-html-->`, () => appHTML)
// ✅ https://github.surmon.me/xxx
// ❌ https://github.surmon.me/xxx/
// 🔗 https://ahrefs.com/blog/trailing-slash/
const fileName = `dist${url === INDEX_ROUTE ? '/index' : url}`
fs.writeFileSync(toAbsolute(`${fileName}.html`), html)
if (url !== INDEX_ROUTE) {
// redirect `x/` to 'x'
fs.mkdirSync(toAbsolute(`${fileName}/`), { recursive: true })
fs.writeFileSync(toAbsolute(`${fileName}/index.html`), renderRedirectionHTML(url))
}
console.log('pre-rendered:', url)
}
fs.rmSync(toAbsolute('dist/ssr'), { recursive: true, force: true })
console.info(`${pageRoutes.length} pages generate done.`)
process.exit(0)
} catch (error) {
console.error('Generate ERROR!', error)
process.exit(1)
}
})()