forked from alexpate/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (65 loc) · 2.19 KB
/
server.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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const next = require('next')
const qs = require('querystring')
const url = require('url')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
function removeEndSlash(fn) {
return (req, res) => {
const parsedUrl = url.parse(req.url, true)
const isNext = parsedUrl.path.includes('/_next/')
if (isNext) return fn(req, res, parsedUrl)
if (parsedUrl.path !== '/' && parsedUrl.path.slice(-1) === '/') {
const q = qs.stringify(parsedUrl.query)
res.writeHead(301, {
Location: parsedUrl.path.slice(0, -1) + (q ? '?' + q : '')
})
res.end()
return
}
return fn(req, res, parsedUrl)
}
}
function setAssetPrefixByHost(fn) {
return (req, res) => {
if (/localhost/.test(req.headers.host)) {
// Set the assetPrefix for localhost
// It needs to be the http version
app.setAssetPrefix(`http://${req.headers.host}`)
} else {
// Set the assetPrefix for now
// It needs to be the https version, since now is always HTTPS
app.setAssetPrefix(`https://${req.headers.host}`)
}
return fn(req, res)
}
}
async function main(req, res, parsedUrl) {
// Redirect to /docs as that's the main page for this zone
if (req.url === '/') {
// 302 as it will be cached by the browser otherwise
res.writeHead(302, {
Location: '/docs'
})
res.end()
return
}
const isNext = parsedUrl.path.includes('/_next/')
// In development we don't cache
// When the user is logged in we don't cache
// When the request is internal to Next.js we call handle immediately as Next.js will handle setting maxage
if (dev || (req.headers.cookie || '').includes('token=') || isNext) {
return handle(req, res, parsedUrl)
}
// s-maxage will cause Now CDN to cache the page for 1 hour (3600 seconds)
res.setHeader('Cache-Control', `public,s-maxage=3600`)
return handle(req, res, parsedUrl)
}
async function setup(handler) {
// Prepare Next.js for bootup
await app.prepare()
// Remove ending slash
return setAssetPrefixByHost(removeEndSlash(handler))
}
// Export a promise that micro will await before starting the server
module.exports = setup(main)