forked from netlify/next-on-netlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupRedirects.js
79 lines (68 loc) · 2.84 KB
/
setupRedirects.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
const path = require('path')
const { join } = path
const { existsSync, readFileSync,
writeFileSync } = require('fs-extra')
const { default: isDynamicRoute } = require("@sls-next/lambda-at-edge/dist/lib/isDynamicRoute")
const { getSortedRoutes } = require("@sls-next/lambda-at-edge/dist/lib/sortedRoutes")
const { NETLIFY_PUBLISH_PATH,
CUSTOM_REDIRECTS_PATH } = require('./config')
const allNextJsPages = require('./allNextJsPages')
const getNetlifyRoute = require('./getNetlifyRoute')
const getNetlifyFunctionName = require('./getNetlifyFunctionName')
// Setup _redirects file that routes all requests to the appropriate location:
// Either the relevant Netlify function or one of the pre-rendered HTML pages.
const setupRedirects = () => {
console.log("\x1b[1m🔀 Setting up redirects\x1b[22m")
// Identify static and dynamically routed pages
const staticPages = allNextJsPages.filter(({ route }) => !isDynamicRoute(route))
const dynamicPages = allNextJsPages.filter(({ route }) => isDynamicRoute(route))
// Sort dynamic pages by route: More-specific routes precede less-specific
// routes
const dynamicRoutes = dynamicPages.map(page => page.route)
const sortedDynamicRoutes = getSortedRoutes(dynamicRoutes)
const sortedDynamicPages = dynamicPages.sort((a, b) => (
sortedDynamicRoutes.indexOf(a.route) - sortedDynamicRoutes.indexOf(b.route)
))
// Assemble sorted pages: static pages first, then sorted dynamic pages
const sortedPages = [...staticPages, ...sortedDynamicPages]
// Generate redirects as array
const redirects = []
if(existsSync(CUSTOM_REDIRECTS_PATH)) {
console.log(" ", "# Prepending custom redirects")
redirects.push(readFileSync(CUSTOM_REDIRECTS_PATH))
}
redirects.push("# Next-on-Netlify Redirects")
sortedPages.forEach(page => {
// Generate redirect for each page route
page.routesAsArray.forEach(route => {
let to
const from = getNetlifyRoute(route)
// SSR pages
if(page.isSsr()) {
to = `/.netlify/functions/${getNetlifyFunctionName(page.filePath)}`
}
// SSG pages
else if (page.isSsg()) {
to = page.htmlFile
}
// SSG fallback pages (for non pre-rendered paths)
else if (page.isSsgFallback()) {
to = `/.netlify/functions/${getNetlifyFunctionName(page.filePath)}`
}
// Pre-rendered HTML pages
else if (page.isHtml()) {
to = `/${path.relative("pages", page.filePath)}`
}
// Assemble redirect
const redirect = `${from} ${to} 200`
console.log(" ", redirect)
redirects.push(redirect)
})
})
// Write redirects to _redirects file
writeFileSync(
join(NETLIFY_PUBLISH_PATH, "_redirects"),
redirects.join("\n")
)
}
module.exports = setupRedirects