Repository navigation
Expand file tree
/
Copy pathprerender.mjs
More file actions
57 lines (48 loc) · 1.81 KB
/
Copy pathprerender.mjs
File metadata and controls
57 lines (48 loc) · 1.81 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
// Prerender script: renders the React app to static HTML for SEO.
// Run after `vite build` via: node prerender.mjs
import { readFileSync, writeFileSync } from 'node:fs'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { createServer } from 'vite'
const __dirname = dirname(fileURLToPath(import.meta.url))
async function main() {
// Use Vite's SSR pipeline to load the server entry (handles TS, CSS, aliases)
const vite = await createServer({
root: __dirname,
server: { middlewareMode: true },
appType: 'custom',
})
try {
const { render } = await vite.ssrLoadModule('/src/entry-server.tsx')
const appHtml = render()
// Read the built client HTML and inject the prerendered content
const distHtmlPath = resolve(__dirname, 'dist/index.html')
let html = readFileSync(distHtmlPath, 'utf-8')
// Replace the empty root div with prerendered content
// The client bundle will hydrate on top of this.
html = html.replace(
/<div id="root"><\/div>/,
`<div id="root">${appHtml}</div>`,
)
writeFileSync(distHtmlPath, html)
// Inject Blog JSON-LD schema for all 50 articles (SEO + AI indexing)
try {
const blogSchema = readFileSync(resolve(__dirname, 'blog-schema.json'), 'utf-8')
html = html.replace(
'</head>',
` <script type="application/ld+json">\n${blogSchema}\n </script>\n</head>`
)
writeFileSync(distHtmlPath, html)
console.log('Injected Blog JSON-LD schema (50 articles)')
} catch (e) {
console.log('Blog schema injection skipped:', e.message)
}
console.log('Prerendered dist/index.html (' + appHtml.length + ' chars of content)')
} finally {
await vite.close()
}
}
main().catch((e) => {
console.error('Prerender failed:', e)
process.exit(1)
})