-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.js
27 lines (23 loc) · 945 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
import fs from 'fs'
import path from 'path'
import {fileURLToPath} from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const absolutePath = (p) => path.resolve(__dirname, p)
const template = fs.readFileSync(absolutePath('dist/client/index.html'), 'utf-8')
const {render} = await import('./dist/server/entry-server.js')
const routesToPrerender = fs
.readdirSync(absolutePath('src/pages'))
.map((file) => {
const name = file.replace(/\.jsx$/, '').toLowerCase()
return name === 'home' ? `/` : `/${name}`
});
(async () => {
for (const url of routesToPrerender) {
const context = {}
const appHtml = render(url, context)
const html = template.replace(`<!--ssr-outlet-->`, appHtml)
const filePath = `dist/client${url === '/' ? '/index' : url}.html`
fs.writeFileSync(absolutePath(filePath), html)
console.log('pre-rendered:', filePath)
}
})()