forked from edgio-docs/edgio-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
142 lines (135 loc) · 4.96 KB
/
routes.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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: 0,
serviceWorkerSeconds: 60 * 60,
},
edge: {
maxAgeSeconds: 60 * 60 * 24 * 365,
staleWhileRevalidateSeconds: 60 * 60 * 24 * 365,
},
}
const redirects = [
['/guides/starter', '/guides/traditional_sites'],
['/guides/debugging', '/guides/troubleshooting'],
['/guides/deploying', '/guides/deploy_apps'],
['/guides/getting_started', '/guides/build_web_apps'],
]
const router = (module.exports = new Router()
// .requireBasicAuth({
// username: process.env.BASIC_AUTH_USERNAME,
// password: process.env.BASIC_AUTH_PASSWORD,
// })
.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 *.intercomcdn.com tr.lfeeder.com data:`,
`frame-src www.youtube.com youtu.be player.vimeo.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 static.hotjar.com s.adroll.com px4.ads.linkedin.com`,
`base-uri 'self'`,
`frame-ancestors 'self'`,
`media-src www.youtube.com`,
`connect-src *.layer0.co *.layer0.link *.layer0-perma.link *.segment.io *.segment.com 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('/:path*', ({ cache }) => {
cache(htmlCacheConfig)
})
// match api docs with a file extension
.match('/docs/api/:path*:file(\\.[css|js|html|json|png]+)', ({ proxy, cache, request }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/current/api/:path*:file' })
})
// match api docs with a terminating /
.match('/docs/api/:path*/', ({ proxy, cache, request }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/current/api/:path*/index.html' })
})
// match api docs without terminating /,
// gets redirected to :path*/ to satisfy relative asset paths
.match('/docs/api/:path*', ({ redirect }) => {
redirect('/docs/api/:path*/')
})
// match versioned api docs with a file extension
.match(
'/docs/:version/api/:path*:file(\\.[css|js|html|json|png]+)',
({ proxy, cache, request }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/:version/api/:path*:file' })
},
)
// match versioned api docs with a terminating /
.match('/docs/:version/api/:path*/', ({ proxy, cache, request }) => {
cache(htmlCacheConfig)
proxy('api', { path: '/:version/api/:path*/index.html' })
})
// match versioned api docs without terminating /,
// gets redirected to :path*/ to satisfy relative asset paths
.match('/docs/:version/api/:path*', ({ redirect }) => {
redirect('/docs/:version/api/:path*/')
})
.get('/googleb2732cddf1383cf4.html', ({ send }) =>
send('google-site-verification: googleb2732cddf1383cf4.html', 200, 'OK'),
))
redirects.forEach(([from, to, statusCode]) => {
router.match(from, ({ redirect }) => redirect(to, statusCode || 302))
})
router.use(nextRoutes).fallback(({ redirect }) => {
return redirect('/', 302)
})