-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathredirects.js
64 lines (52 loc) · 2.14 KB
/
redirects.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
const { join } = require('path')
const slash = require('slash')
const addDefaultLocaleRedirect = require('../../helpers/addDefaultLocaleRedirect')
const asyncForEach = require('../../helpers/asyncForEach')
const getFilePathForRoute = require('../../helpers/getFilePathForRoute')
const getNetlifyFunctionName = require('../../helpers/getNetlifyFunctionName')
const getPages = require('./pages')
// getStaticProps pages
//
// Page params {
// route -> '/getStaticProps', '/getStaticProps/3'
// dataRoute -> '/_next/data/{BUILD_ID}/getStaticProps.json', '_next/data/{BUILD_ID}/getStaticProps/3.json'
// srcRoute -> null, /getStaticProps/[id]
// }
//
// Page params with i18n {
// route -> '/getStaticProps', '/en/getStaticProps/3'
// dataRoute -> '/_next/data/{BUILD_ID}/getStaticProps.json', '_next/data/{BUILD_ID}/en/getStaticProps/3.json'
// srcRoute -> null, /getStaticProps/[id]
// }
//
//
// Pages with getStaticProps (without fallback or revalidation) only need
// redirects for i18n and handling preview mode
const getRedirects = async () => {
const redirects = []
const pages = await getPages()
await asyncForEach(pages, async ({ route, dataRoute, srcRoute }) => {
const relativePath = getFilePathForRoute(srcRoute || route, 'js')
const filePath = slash(join('pages', relativePath))
const functionName = getNetlifyFunctionName(filePath)
// Preview mode conditions
const conditions = ['Cookie=__prerender_bypass,__next_preview_data']
const target = `/.netlify/functions/${functionName}`
const previewModeRedirect = { conditions, force: true, target }
// Add a preview mode redirect for the standard route
redirects.push({
route,
...previewModeRedirect,
})
// Add a preview mode redirect for the data route, same conditions
redirects.push({
route: dataRoute,
...previewModeRedirect,
})
// Preview mode default locale redirect must precede normal default locale redirect
await addDefaultLocaleRedirect(redirects, route, target, previewModeRedirect)
await addDefaultLocaleRedirect(redirects, route)
})
return redirects
}
module.exports = getRedirects