This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathgetNetlifyRoutes.js
58 lines (47 loc) · 2.08 KB
/
getNetlifyRoutes.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
// Adapted from @sls-next/lambda-at-edge (v1.2.0-alpha.3)
// See: https://github.com/danielcondemarin/serverless-next.js/blob/57142970b08e6bc3faf0fc70749b3b0501ad7869/packages/lambda-at-edge/src/lib/expressifyDynamicRoute.ts#L4
const {
CATCH_ALL_REGEX,
OPTIONAL_CATCH_ALL_REGEX,
DYNAMIC_PARAMETER_REGEX,
} = require("../constants/regex");
// Convert dynamic NextJS routes into their Netlify-equivalent
// Note that file endings (.json) must be removed for catch-all and optional
// catch-all routes to work. That's why the regexes defined above include
// the (.json)? option
const getNetlifyRoutes = (nextRoute) => {
let netlifyRoutes = [nextRoute];
// If the route is an optional catch-all route, we need to add a second
// Netlify route for the base path (when no parameters are present).
// The file ending must be present!
if (nextRoute.match(OPTIONAL_CATCH_ALL_REGEX)) {
let netlifyRoute = nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, "$2");
// When optional catch-all route is at top-level, the regex on line 19 will
// create an empty string, but actually needs to be a forward slash
if (netlifyRoute === "") netlifyRoute = "/";
// When optional catch-all route is at top-level, the regex on line 19 will
// create an incorrect route for the data route. For example, it creates
// /_next/data/%BUILDID%.json, but NextJS looks for
// /_next/data/%BUILDID%/index.json
netlifyRoute = netlifyRoute.replace(
/(\/_next\/data\/[^/]+).json/,
"$1/index.json"
);
// Add second route to the front of the array
netlifyRoutes.unshift(netlifyRoute);
}
// Replace catch-all, e.g., [...slug]
netlifyRoutes = netlifyRoutes.map((route) =>
route.replace(CATCH_ALL_REGEX, "/:$1/*")
);
// Replace optional catch-all, e.g., [[...slug]]
netlifyRoutes = netlifyRoutes.map((route) =>
route.replace(OPTIONAL_CATCH_ALL_REGEX, "/*")
);
// Replace dynamic parameters, e.g., [id]
netlifyRoutes = netlifyRoutes.map((route) =>
route.replace(DYNAMIC_PARAMETER_REGEX, "/:$1")
);
return netlifyRoutes;
};
module.exports = getNetlifyRoutes;