forked from edgio-docs/edgio-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
113 lines (109 loc) · 3.74 KB
/
Copy pathroutes.js
File metadata and controls
113 lines (109 loc) · 3.74 KB
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
const { Router, CustomCacheKey } = require('@layer0/core/router')
const { nextRoutes } = require('@layer0/next')
// const { isProductionBuild } = require('../layer0/packages/core/src/environment')
const key = new CustomCacheKey().excludeAllQueryParametersExcept('query', 'version')
const prerenderRequests = require('./layer0/prerenderRequests')
const htmlCacheConfig = {
key,
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 0,
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365,
},
}
const apiCacheConfig = {
key,
browser: {
maxAgeSeconds: 0,
serviceWorkerSeconds: 60 * 60,
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365,
},
}
const staticCacheConfig = {
key,
browser: {
maxAgeSeconds: 60 * 60,
serviceWorkerSeconds: 60 * 60,
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365,
},
}
module.exports = new Router()
.prerender(prerenderRequests)
.match({}, ({ setResponseHeader }) => {
if (process.env.NODE_ENV === 'production') {
setResponseHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload')
setResponseHeader(
'Content-Security-Policy',
[
`default-src 'self'`,
`style-src 'unsafe-inline' 'self' fonts.googleapis.com cdn.jsdelivr.net`,
`font-src fonts.gstatic.com`,
`img-src 'self' www.google-analytics.com analytics.twitter.com www.facebook.com px.ads.linkedin.com tr.lfeeder.com data:`,
`frame-src www.youtube.com`,
`script-src 'unsafe-inline' 'self' 'unsafe-eval' cdn.jsdelivr.net www.googletagmanager.com cdn.segment.com cdn4.mxpnl.com www.google-analytics.com widget.intercom.io sc.lfeeder.com snap.licdn.com connect.facebook.net www.youtube.com js.intercomcdn.com`,
`base-uri 'self'`,
`frame-ancestors 'self'`,
`media-src www.youtube.com`,
`connect-src *.layer0.co *.segment.io analytics.google.com *.intercom.io *.intercomcdn.com *.intercomassets.com *.github.io *.algolianet.com *.algolia.net`,
].join('; '),
)
setResponseHeader('X-XSS-Protection', '1; mode=block')
}
})
.match('/service-worker.js', ({ cache, serveStatic }) => {
cache({
browser: {
maxAgeSeconds: 0,
},
edge: {
maxAgeSeconds: 60 * 60 * 365,
},
})
serveStatic('.next/static/service-worker.js')
})
.get('/images/:path*', ({ cache }) => {
cache(staticCacheConfig)
})
// the following route is needed for older guides and should not be removed
.match('/guides/images/:path*', ({ cache, serveStatic }) => {
cache(staticCacheConfig)
serveStatic('public/images/:path*')
})
.match('/api/:path*', ({ cache }) => {
cache(apiCacheConfig)
})
.match('/:path*', ({ cache }) => {
cache(htmlCacheConfig)
})
.match('/docs/api/:path*/', ({ proxy, cache }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/layer0-docs-pages/current/api/:path*/' })
})
.match('/docs/api/:path*', ({ proxy, cache }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/layer0-docs-pages/current/api/:path*' })
})
.match('/docs/:version/api/:path*/', ({ proxy, cache }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/layer0-docs-pages/:version/api/:path*/' })
})
.match('/docs/:version/api/:path*', ({ proxy, cache }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/layer0-docs-pages/:version/api/:path*' })
})
.get('/googleb2732cddf1383cf4.html', ({ send }) =>
send('google-site-verification: googleb2732cddf1383cf4.html', 200, 'OK'),
)
.use(nextRoutes)
.fallback(({ redirect }) => {
return redirect('/', 302)
})