/** * Postbuild: turn each prerendered /errors/ route into a self-contained * HTML file at build/client/errors/.html for nginx error_page use. * * - Inlines every by replacing it with `; }); // Drop module preloads and module scripts — nothing should hydrate. html = html.replace(/]*\brel=["']modulepreload["'][^>]*>\s*/g, ''); html = html.replace(/]*\btype=["']module["'][^>]*>[\s\S]*?<\/script>\s*/g, ''); // Point the brand/home link at the canonical site (the page may be served // from any domain when used as nginx's default_server fallback). html = html.replace(/]*\bclass="[^"]*\bhome-link\b[^"]*"[^>]*>/g, (tag) => tag.replace(/\bhref="[^"]*"/, `href="${CANONICAL_HOME}"`) ); return html; } for (const { rel, abs } of sources) { const dst = join(OUT_DIR, rel); mkdirSync(dirname(dst), { recursive: true }); const html = inline(readFileSync(abs, 'utf8'), `/errors/${rel}`); const buf = Buffer.from(html, 'utf8'); writeFileSync(dst, buf); writeFileSync(`${dst}.gz`, gzipSync(buf, { level: 9 })); writeFileSync(`${dst}.br`, brotliCompressSync(buf, { params: { [zlib.BROTLI_PARAM_QUALITY]: 11 } })); console.log(`[error-page] wrote errors/${rel} (${(buf.length / 1024).toFixed(1)} kB) + .gz + .br`); }