-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-static.js
More file actions
59 lines (44 loc) · 1.78 KB
/
generate-static.js
File metadata and controls
59 lines (44 loc) · 1.78 KB
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
import * as fs from 'node:fs'
import * as path from 'node:path'
function wait(delayInMs) {
return new Promise((resolve) => {
setTimeout(resolve, delayInMs)
})
}
// see src/App.tsx for roots
const pagesForPreRendering = ['/', '/docs', '/maps', '/mods', '/pocs', '/tools']
const distPath = path.resolve('dist')
const template = fs.readFileSync(path.resolve(distPath, 'client/index.html'), 'utf-8')
async function generatePage(route) {
const cleanRoute = route.replaceAll('?', '_').replaceAll('%20', '-') // Replace "?" with "_" for filenames
const { render } = await import('./dist/server/entry-server.js')
const html = render(route)
// clone the template with template.html [this file if that page not required SSG then SSR will use]
if (route === '/') {
const filePath = path.join(`${distPath}/client`, 'template.html')
console.log(`✅ Generated: ${filePath}`)
fs.writeFileSync(filePath, template, 'utf-8')
}
const head = html.slice(html.indexOf('<head>') + 6, html.indexOf('</head>'))
const body = html.slice(head.length + '<head>'.length + '</head>'.length)
// Inject head and body content properly
const outputHtml = template.replace('<!-- head -->', head).replace('<!-- body -->', body)
let filePath
if (route === '/') {
filePath = path.join(`${distPath}/client`, 'index.html')
} else {
filePath = path.join(`${distPath}/client`, `${cleanRoute}/index.html`)
}
if (!fs.existsSync(path.dirname(filePath))) {
fs.mkdirSync(path.dirname(filePath), { recursive: true })
}
fs.writeFileSync(filePath, outputHtml, 'utf-8')
console.log(`✅ Generated: ${filePath}`)
}
async function generatePagesSequentially() {
for (const route of pagesForPreRendering) {
await generatePage(route)
await wait(100)
}
}
generatePagesSequentially()