-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.ts
263 lines (231 loc) · 8.19 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* eslint-disable max-lines */
import type { NetlifyConfig } from '@netlify/build'
import type { Header } from '@netlify/build/types/config/netlify_config'
import globby from 'globby'
import type { ExperimentalConfig } from 'next/dist/server/config-shared'
import type { ImageConfigComplete, RemotePattern } from 'next/dist/shared/lib/image-config'
import { join } from 'pathe'
import { OPTIONAL_CATCH_ALL_REGEX, CATCH_ALL_REGEX, DYNAMIC_PARAMETER_REGEX, HANDLER_FUNCTION_PATH } from '../constants'
import { ApiRouteType } from './analysis'
import type { ApiRouteConfig } from './functions'
import { I18n } from './types'
const RESERVED_FILENAME = /[^\w_-]/g
/**
* Given a Next route, generates a valid Netlify function name.
* If "background" is true then the function name will have `-background`
* appended to it, meaning that it is executed as a background function.
*/
export const getFunctionNameForPage = (page: string, background = false) =>
`${page
.replace(CATCH_ALL_REGEX, '_$1-SPLAT')
.replace(OPTIONAL_CATCH_ALL_REGEX, '-SPLAT')
.replace(DYNAMIC_PARAMETER_REGEX, '_$1-PARAM')
.replace(RESERVED_FILENAME, '_')}-${background ? 'background' : 'handler'}`
type ExperimentalConfigWithLegacy = ExperimentalConfig & {
images?: Pick<ImageConfigComplete, 'remotePatterns'>
}
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 is404Route = (route: string, i18n?: I18n) =>
i18n ? i18n.locales.some((locale) => route === `/${locale}/404`) : route === '/404'
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: string, apiRoutes: Array<ApiRouteConfig>) => {
const apiRewrites = apiRoutes.map((apiRoute) => {
const [from] = toNetlifyRoute(`${basePath}${apiRoute.route}`)
// Scheduled functions can't be invoked directly, so we 404 them.
if (apiRoute.config.type === ApiRouteType.SCHEDULED) {
return { from, to: '/404.html', status: 404 }
}
return {
from,
to: `/.netlify/functions/${getFunctionNameForPage(
apiRoute.route,
apiRoute.config.type === ApiRouteType.BACKGROUND,
)}`,
status: 200,
}
})
return [
...apiRewrites,
{
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
}
export const isNextAuthInstalled = (): boolean => {
try {
// eslint-disable-next-line import/no-unassigned-import, import/no-extraneous-dependencies, n/no-extraneous-require
require('next-auth')
return true
} catch {
// Ignore the MODULE_NOT_FOUND error
return false
}
}
export const getCustomImageResponseHeaders = (headers: Header[]): Record<string, string> | null => {
const customImageResponseHeaders = headers.find((header) => header.for?.startsWith('/_next/image/'))
if (customImageResponseHeaders) {
return customImageResponseHeaders?.values as Record<string, string>
}
return null
}
export const isBundleSizeCheckDisabled = () =>
process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK === '1' || process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK === 'true'
// In v12.2.6-canary.12 the types had not yet been updated.
// Once this type is available from the next package, this should
// be removed
export type ImagesConfig = Partial<ImageConfigComplete> &
Required<ImageConfigComplete> & {
remotePatterns?: RemotePattern[]
}
export const getRemotePatterns = (experimental: ExperimentalConfigWithLegacy, images: ImagesConfig) => {
// Where remote patterns is configured pre-v12.2.5
if (experimental.images?.remotePatterns) {
return experimental.images.remotePatterns
}
// Where remote patterns is configured after v12.2.5
if (images.remotePatterns) {
return images.remotePatterns || []
}
return []
}
/* eslint-enable max-lines */