-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathredirects.js
51 lines (42 loc) · 1.45 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
const getNextConfig = require('../../../../helpers/getNextConfig')
const addDefaultLocaleRedirect = require('../../helpers/addDefaultLocaleRedirect')
const asyncForEach = require('../../helpers/asyncForEach')
const isDynamicRoute = require('../../helpers/isDynamicRoute')
const getPages = require('./pages')
// withoutProps pages
//
// Page params {
// route -> '/about', '/initial/[id]'
// filePath -> 'pages/about.html', 'pages/initial[id].html'
// }
//
// Page params in i18n {
// route -> '/en/about', '/fr/initial/[id]'
// filePath -> 'pages/en/about.html', 'pages/fr/initial[id].html'
// }
//
const getRedirects = async () => {
const redirects = []
const pages = await getPages()
const { basePath } = await getNextConfig()
await asyncForEach(pages, async ({ route, filePath }) => {
const target = filePath.replace(/pages/, '')
await addDefaultLocaleRedirect(redirects, route, target)
// For sites that use basePath, manually add necessary redirects here specific
// only to this page type (which excludes static route redirects by default)
if (basePath && basePath !== '') {
redirects.push({
route: `${basePath}${route}`,
target: route,
})
}
// Only create normal redirects for pages with dynamic routing
if (!isDynamicRoute(route)) return
redirects.push({
route,
target: filePath.replace(/pages/, ''),
})
})
return redirects
}
module.exports = getRedirects