1
- // @ts -check
1
+ /* eslint-disable max-lines */
2
2
const { cpus } = require ( 'os' )
3
3
4
4
const { yellowBright } = require ( 'chalk' )
@@ -28,10 +28,36 @@ const matchMiddleware = (middleware, filePath) =>
28
28
filePath === middlewarePath || filePath === `${ middlewarePath } .html` || filePath . startsWith ( `${ middlewarePath } /` ) ,
29
29
)
30
30
31
+ const matchesRedirect = ( file , redirects ) => {
32
+ if ( ! Array . isArray ( redirects ) ) {
33
+ return false
34
+ }
35
+ return redirects . some ( ( redirect ) => {
36
+ if ( ! redirect . regex || redirect . internal ) {
37
+ return false
38
+ }
39
+ // Strips the extension from the file path
40
+ return new RegExp ( redirect . regex ) . test ( `/${ file . slice ( 0 , - 5 ) } ` )
41
+ } )
42
+ }
43
+
44
+ const matchesRewrite = ( file , rewrites ) => {
45
+ if ( Array . isArray ( rewrites ) ) {
46
+ return matchesRedirect ( file , rewrites )
47
+ }
48
+ if ( ! Array . isArray ( rewrites ?. beforeFiles ) ) {
49
+ return false
50
+ }
51
+ return matchesRedirect ( file , rewrites . beforeFiles )
52
+ }
53
+
54
+ exports . matchesRedirect = matchesRedirect
55
+ exports . matchesRewrite = matchesRewrite
31
56
exports . matchMiddleware = matchMiddleware
32
57
exports . stripLocale = stripLocale
33
58
exports . isDynamicRoute = isDynamicRoute
34
59
60
+ // eslint-disable-next-line max-lines-per-function
35
61
exports . moveStaticPages = async ( { netlifyConfig, target, i18n } ) => {
36
62
console . log ( 'Moving static page files to serve from CDN...' )
37
63
const outputDir = join ( netlifyConfig . build . publish , target === 'server' ? 'server' : 'serverless' )
@@ -48,6 +74,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
48
74
}
49
75
50
76
const prerenderManifest = await readJson ( join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) )
77
+ const { redirects, rewrites } = await readJson ( join ( netlifyConfig . build . publish , 'routes-manifest.json' ) )
51
78
52
79
const isrFiles = new Set ( )
53
80
@@ -79,6 +106,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
79
106
80
107
const matchingMiddleware = new Set ( )
81
108
const matchedPages = new Set ( )
109
+ const matchedRedirects = new Set ( )
110
+ const matchedRewrites = new Set ( )
82
111
83
112
// Limit concurrent file moves to number of cpus or 2 if there is only 1
84
113
const limit = pLimit ( Math . max ( 2 , cpus ( ) . length ) )
@@ -91,6 +120,14 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
91
120
if ( isDynamicRoute ( filePath ) ) {
92
121
return
93
122
}
123
+ if ( matchesRedirect ( filePath , redirects ) ) {
124
+ matchedRedirects . add ( filePath )
125
+ return
126
+ }
127
+ if ( matchesRewrite ( filePath , rewrites ) ) {
128
+ matchedRewrites . add ( filePath )
129
+ return
130
+ }
94
131
// Middleware matches against the unlocalised path
95
132
const unlocalizedPath = stripLocale ( rawPath , i18n ?. locales )
96
133
const middlewarePath = matchMiddleware ( middleware , unlocalizedPath )
@@ -110,7 +147,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
110
147
yellowBright ( outdent `
111
148
Skipped moving ${ matchedPages . size } ${
112
149
matchedPages . size === 1 ? 'file because it matches' : 'files because they match'
113
- } middleware, so cannot be deployed to the CDN and will be served from the origin instead. This is fine, but we're letting you know because it may not be what you expect.
150
+ } middleware, so cannot be deployed to the CDN and will be served from the origin instead.
151
+ This is fine, but we're letting you know because it may not be what you expect.
114
152
` ) ,
115
153
)
116
154
@@ -119,6 +157,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
119
157
The following middleware matched statically-rendered pages:
120
158
121
159
${ yellowBright ( [ ...matchingMiddleware ] . map ( ( mid ) => `- /${ mid } /_middleware` ) . join ( '\n' ) ) }
160
+
161
+ ────────────────────────────────────────────────────────────────
122
162
` ,
123
163
)
124
164
// There could potentially be thousands of matching pages, so we don't want to spam the console with this
@@ -128,6 +168,40 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
128
168
The following files matched middleware and were not moved to the CDN:
129
169
130
170
${ yellowBright ( [ ...matchedPages ] . map ( ( mid ) => `- ${ mid } ` ) . join ( '\n' ) ) }
171
+
172
+ ────────────────────────────────────────────────────────────────
173
+ ` ,
174
+ )
175
+ }
176
+ }
177
+
178
+ if ( matchedRedirects . size !== 0 || matchedRewrites . size !== 0 ) {
179
+ console . log (
180
+ yellowBright ( outdent `
181
+ Skipped moving ${
182
+ matchedRedirects . size + matchedRewrites . size
183
+ } files because they match redirects or beforeFiles rewrites, so cannot be deployed to the CDN and will be served from the origin instead.
184
+ ` ) ,
185
+ )
186
+ if ( matchedRedirects . size < 50 && matchedRedirects . size !== 0 ) {
187
+ console . log (
188
+ outdent `
189
+ The following files matched redirects and were not moved to the CDN:
190
+
191
+ ${ yellowBright ( [ ...matchedRedirects ] . map ( ( mid ) => `- ${ mid } ` ) . join ( '\n' ) ) }
192
+
193
+ ────────────────────────────────────────────────────────────────
194
+ ` ,
195
+ )
196
+ }
197
+ if ( matchedRewrites . size < 50 && matchedRewrites . size !== 0 ) {
198
+ console . log (
199
+ outdent `
200
+ The following files matched beforeFiles rewrites and were not moved to the CDN:
201
+
202
+ ${ yellowBright ( [ ...matchedRewrites ] . map ( ( mid ) => `- ${ mid } ` ) . join ( '\n' ) ) }
203
+
204
+ ────────────────────────────────────────────────────────────────
131
205
` ,
132
206
)
133
207
}
@@ -151,3 +225,4 @@ exports.movePublicFiles = async ({ appDir, publish }) => {
151
225
await copy ( publicDir , `${ publish } /` )
152
226
}
153
227
}
228
+ /* eslint-enable max-lines */
0 commit comments