generated from antfu-collective/vitesse
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
30 lines (25 loc) · 846 Bytes
/
index.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
28
29
30
const path = require('path')
const manifest = require(path.join(__dirname, 'renderer', 'ssr-manifest.json'))
const render = require(path.join(__dirname, 'renderer', 'main.js')).default
// Polyfill
globalThis.fetch = require('node-fetch')
module.exports = async (req, res) => {
try {
const protocol =
req.protocol || (req.headers.referer || '').split(':')[0] || 'http'
const host = process.env.VERCEL_URL
const url = protocol + '://' + host + req.url
const { html } = await render(url, { manifest, preload: true })
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.setHeader(
'Cache-Control',
'max-age=0, s-maxage=86400, stale-while-revalidate'
)
res.end(html)
} catch (error) {
console.error(error.stack)
res.statusCode = 500
res.end(error.stack)
}
}