-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathutils.ts
41 lines (35 loc) · 1.63 KB
/
utils.ts
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
import { OPTIONAL_CATCH_ALL_REGEX, CATCH_ALL_REGEX, DYNAMIC_PARAMETER_REGEX } from '../constants'
export const netlifyRoutesForNextRoute = (nextRoute: string): Array<string> => {
const 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 (OPTIONAL_CATCH_ALL_REGEX.test(nextRoute)) {
let netlifyRoute = nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, '$2')
// 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)
}
return netlifyRoutes.map((route) =>
route
// Replace catch-all, e.g., [...slug]
.replace(CATCH_ALL_REGEX, '/:$1/*')
// Replace optional catch-all, e.g., [[...slug]]
.replace(OPTIONAL_CATCH_ALL_REGEX, '/*')
// Replace dynamic parameters, e.g., [id]
.replace(DYNAMIC_PARAMETER_REGEX, '/:$1'),
)
}
export const shouldSkip = (): boolean =>
process.env.NEXT_PLUGIN_FORCE_RUN === 'false' ||
process.env.NEXT_PLUGIN_FORCE_RUN === '0' ||
process.env.NETLIFY_NEXT_PLUGIN_SKIP === 'true' ||
process.env.NETLIFY_NEXT_PLUGIN_SKIP === '1'