1- // @ts -check
1+ /* eslint-disable max-lines */
22const { cpus } = require ( 'os' )
33
44const { yellowBright } = require ( 'chalk' )
@@ -28,10 +28,36 @@ const matchMiddleware = (middleware, filePath) =>
2828 filePath === middlewarePath || filePath === `${ middlewarePath } .html` || filePath . startsWith ( `${ middlewarePath } /` ) ,
2929 )
3030
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
3156exports . matchMiddleware = matchMiddleware
3257exports . stripLocale = stripLocale
3358exports . isDynamicRoute = isDynamicRoute
3459
60+ // eslint-disable-next-line max-lines-per-function
3561exports . moveStaticPages = async ( { netlifyConfig, target, i18n } ) => {
3662 console . log ( 'Moving static page files to serve from CDN...' )
3763 const outputDir = join ( netlifyConfig . build . publish , target === 'server' ? 'server' : 'serverless' )
@@ -48,6 +74,7 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
4874 }
4975
5076 const prerenderManifest = await readJson ( join ( netlifyConfig . build . publish , 'prerender-manifest.json' ) )
77+ const { redirects, rewrites } = await readJson ( join ( netlifyConfig . build . publish , 'routes-manifest.json' ) )
5178
5279 const isrFiles = new Set ( )
5380
@@ -79,6 +106,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
79106
80107 const matchingMiddleware = new Set ( )
81108 const matchedPages = new Set ( )
109+ const matchedRedirects = new Set ( )
110+ const matchedRewrites = new Set ( )
82111
83112 // Limit concurrent file moves to number of cpus or 2 if there is only 1
84113 const limit = pLimit ( Math . max ( 2 , cpus ( ) . length ) )
@@ -91,6 +120,14 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
91120 if ( isDynamicRoute ( filePath ) ) {
92121 return
93122 }
123+ if ( matchesRedirect ( filePath , redirects ) ) {
124+ matchedRedirects . add ( filePath )
125+ return
126+ }
127+ if ( matchesRewrite ( filePath , rewrites ) ) {
128+ matchedRewrites . add ( filePath )
129+ return
130+ }
94131 // Middleware matches against the unlocalised path
95132 const unlocalizedPath = stripLocale ( rawPath , i18n ?. locales )
96133 const middlewarePath = matchMiddleware ( middleware , unlocalizedPath )
@@ -110,7 +147,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
110147 yellowBright ( outdent `
111148 Skipped moving ${ matchedPages . size } ${
112149 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.
114152 ` ) ,
115153 )
116154
@@ -119,6 +157,8 @@ exports.moveStaticPages = async ({ netlifyConfig, target, i18n }) => {
119157 The following middleware matched statically-rendered pages:
120158
121159 ${ yellowBright ( [ ...matchingMiddleware ] . map ( ( mid ) => `- /${ mid } /_middleware` ) . join ( '\n' ) ) }
160+
161+ ────────────────────────────────────────────────────────────────
122162 ` ,
123163 )
124164 // 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 }) => {
128168 The following files matched middleware and were not moved to the CDN:
129169
130170 ${ 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+ ────────────────────────────────────────────────────────────────
131205 ` ,
132206 )
133207 }
@@ -151,3 +225,4 @@ exports.movePublicFiles = async ({ appDir, publish }) => {
151225 await copy ( publicDir , `${ publish } /` )
152226 }
153227}
228+ /* eslint-enable max-lines */
0 commit comments