-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.ts
176 lines (157 loc) · 5.06 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { NetlifyConfig } from '@netlify/build'
import globby from 'globby'
import { join } from 'pathe'
import { OPTIONAL_CATCH_ALL_REGEX, CATCH_ALL_REGEX, DYNAMIC_PARAMETER_REGEX, HANDLER_FUNCTION_PATH } from '../constants'
import { I18n } from './types'
export const toNetlifyRoute = (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 netlifyRoutesForNextRouteWithData = ({ route, dataRoute }: { route: string; dataRoute: string }) => [
...toNetlifyRoute(dataRoute),
...toNetlifyRoute(route),
]
export const routeToDataRoute = (route: string, buildId: string, locale?: string) =>
`/_next/data/${buildId}${locale ? `/${locale}` : ''}${route === '/' ? '/index' : route}.json`
const netlifyRoutesForNextRoute = (route: string, buildId: string, i18n?: I18n): Array<string> => {
if (!i18n?.locales?.length) {
return netlifyRoutesForNextRouteWithData({ route, dataRoute: routeToDataRoute(route, buildId) })
}
const { locales, defaultLocale } = i18n
const routes = []
locales.forEach((locale) => {
// Data route is always localized
const dataRoute = routeToDataRoute(route, buildId, locale)
routes.push(
// Default locale is served from root, not localized
...netlifyRoutesForNextRouteWithData({
route: locale === defaultLocale ? route : `/${locale}${route}`,
dataRoute,
}),
)
})
return routes
}
export const isApiRoute = (route: string) => route.startsWith('/api/') || route === '/api'
export const redirectsForNextRoute = ({
route,
buildId,
basePath,
to,
i18n,
status = 200,
force = false,
}: {
route: string
buildId: string
basePath: string
to: string
i18n: I18n
status?: number
force?: boolean
}): NetlifyConfig['redirects'] =>
netlifyRoutesForNextRoute(route, buildId, i18n).map((redirect) => ({
from: `${basePath}${redirect}`,
to,
status,
force,
}))
export const redirectsForNextRouteWithData = ({
route,
dataRoute,
basePath,
to,
status = 200,
force = false,
}: {
route: string
dataRoute: string
basePath: string
to: string
status?: number
force?: boolean
}): NetlifyConfig['redirects'] =>
netlifyRoutesForNextRouteWithData({ route, dataRoute }).map((redirect) => ({
from: `${basePath}${redirect}`,
to,
status,
force,
}))
export const getApiRewrites = (basePath) => [
{
from: `${basePath}/api`,
to: HANDLER_FUNCTION_PATH,
status: 200,
},
{
from: `${basePath}/api/*`,
to: HANDLER_FUNCTION_PATH,
status: 200,
},
]
export const getPreviewRewrites = async ({ basePath, appDir }) => {
const publicFiles = await globby('**/*', { cwd: join(appDir, 'public') })
// Preview mode gets forced to the function, to bypass pre-rendered pages, but static files need to be skipped
return [
...publicFiles.map((file) => ({
from: `${basePath}/${file}`,
// This is a no-op, but we do it to stop it matching the following rule
to: `${basePath}/${file}`,
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
status: 200,
})),
{
from: `${basePath}/*`,
to: HANDLER_FUNCTION_PATH,
status: 200,
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
force: true,
},
]
}
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'
/**
* Given an array of base paths and candidate modules, return the first one that exists
*/
export const findModuleFromBase = ({ paths, candidates }): string | null => {
for (const candidate of candidates) {
try {
const modulePath = require.resolve(candidate, { paths })
if (modulePath) {
return modulePath
}
} catch {
// Ignore the error
}
}
return null
}