-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathredirects.js
55 lines (44 loc) · 1.62 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
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 with revalidate 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 in 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]
// }
//
const getRedirects = async () => {
const redirects = []
const pages = await getPages()
await asyncForEach(pages, async ({ route, srcRoute, dataRoute }) => {
const relativePath = getFilePathForRoute(srcRoute || route, 'js')
const filePath = slash(join('pages', relativePath))
const functionName = getNetlifyFunctionName(filePath)
const target = `/.netlify/functions/${functionName}`
// Add one redirect for the page
redirects.push({
route,
target,
})
// Add one redirect for the data route
redirects.push({
route: dataRoute,
target,
})
await addDefaultLocaleRedirect(redirects, route, target)
})
return redirects
}
module.exports = getRedirects