-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.cjs
121 lines (106 loc) · 3.32 KB
/
routes.cjs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { brotliCompressSync } from 'zlib'
import { Router } from '@edgio/core/router'
import { isProductionBuild } from '@edgio/core/environment'
import CustomCacheKey from '@edgio/core/router/CustomCacheKey'
const BROTLI_ENCODING_REGEX = /\bbr\b/
const sendBrotliEncoded = (req, res) => {
const acceptEncoding = req.getHeader('accept-encoding')
const acceptBrotliEncoding = BROTLI_ENCODING_REGEX.test(acceptEncoding)
if (!acceptBrotliEncoding) return
const encodedBody = brotliCompressSync(Buffer.from(res.body))
res.setHeader('content-length', Buffer.byteLength(encodedBody))
res.setHeader('content-encoding', 'br')
res.body = encodedBody
}
// Try to brotli encode each response
const transformResponse = (res, req) => {
sendBrotliEncoded(req, res)
}
const router = new Router()
if (isProductionBuild()) {
router.static('.vercel/output/static')
}
// Preload the URLs as soon as the deployment is done
router.prerender(async () => {
return [{ path: '/' }, { path: '/__data.json' }]
})
router.match('/service-worker.js', ({ serviceWorker }) => {
serviceWorker('.edgio/tmp/service-worker.js')
})
// Cache the page repsonses at the edge only
router.match('/', ({ renderWithApp, removeUpstreamResponseHeader, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
edge: {
maxAgeSeconds: 60 * 60,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365
},
key: new CustomCacheKey().excludeAllQueryParametersExcept('keyName', 'search', 'toggle')
})
renderWithApp({ transformResponse })
})
router.match('/templates/:path', ({ renderWithApp, removeUpstreamResponseHeader, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
edge: {
maxAgeSeconds: 60 * 60,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365
},
key: new CustomCacheKey().excludeAllQueryParametersExcept('keyName', 'search')
})
renderWithApp({ transformResponse })
})
// Cache the navigation JSONs at the browser in SW only
router.match('/__data.json', ({ renderWithApp, removeUpstreamResponseHeader, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
browser: {
serviceWorkerSeconds: 60
},
edge: {
maxAgeSeconds: 60 * 60,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365
},
key: new CustomCacheKey().excludeAllQueryParametersExcept('keyName', 'search', 'toggle')
})
renderWithApp({ transformResponse })
})
router.match('/templates/:path/__data.json', ({ renderWithApp, removeUpstreamResponseHeader, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
browser: {
serviceWorkerSeconds: 60
},
edge: {
maxAgeSeconds: 60 * 60,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365
},
key: new CustomCacheKey().excludeAllQueryParametersExcept('keyName', 'search')
})
renderWithApp({ transformResponse })
})
// Only match requests to the /og which contain text, image and description
router.match(
{
path: '/og',
query: {
text: /^.{1,}$/,
image: /^.{1,}$/,
description: /^.{1,}$/
}
},
({ renderWithApp, removeUpstreamResponseHeader, cache }) => {
removeUpstreamResponseHeader('cache-control')
cache({
browser: {
maxAgeSeconds: 60
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365
},
key: new CustomCacheKey().excludeAllQueryParametersExcept('width', 'height', 'text', 'description', 'image')
})
renderWithApp({ transformResponse })
}
)
export default router