Files
sag24-website/scripts/prerender.mjs

35 lines
1.2 KiB
JavaScript

import { readFileSync, writeFileSync, rmSync, readdirSync } from 'fs'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const root = resolve(__dirname, '..')
const buildDir = process.env.BUILD_DIR || 'dist'
// Import SSR bundle built by vite build --ssr
const { render } = await import(`../${buildDir}/server/entry-server.js`)
let html = readFileSync(resolve(root, buildDir, 'index.html'), 'utf-8')
// Inject prerendered HTML
const appHtml = render()
html = html.replace('<div id="root"></div>', `<div id="root">${appHtml}</div>`)
// Fix preload font: find actual cyrillic-700 woff2 hash in dist/assets
const assetsDir = resolve(root, buildDir, 'assets')
const fontFile = readdirSync(assetsDir).find(f => f.match(/manrope-cyrillic-700-normal-.+\.woff2/))
if (fontFile) {
html = html.replace(
/href="\/assets\/manrope-cyrillic-700-normal-[^"]+\.woff2"/,
`href="/assets/${fontFile}"`
)
console.log(`✓ Preload font updated: ${fontFile}`)
}
writeFileSync(resolve(root, buildDir, 'index.html'), html)
// Cleanup SSR bundle
rmSync(resolve(root, buildDir, 'server'), { recursive: true, force: true })
console.log('✓ Prerendered index.html')