@@ -4,6 +4,7 @@ const { logTitle, logItem } = require("../helpers/logger");
44const { NETLIFY_PUBLISH_PATH , CUSTOM_REDIRECTS_PATH } = require ( "../config" ) ;
55const getSortedRoutes = require ( "../helpers/getSortedRoutes" ) ;
66const getNetlifyRoutes = require ( "../helpers/getNetlifyRoutes" ) ;
7+ const isRootCatchAllRedirect = require ( "../helpers/isRootCatchAllRedirect" ) ;
78
89// Setup _redirects file that routes all requests to the appropriate location,
910// such as one of the Netlify functions or one of the static files.
@@ -14,7 +15,7 @@ const setupRedirects = () => {
1415 const redirects = [ ] ;
1516 if ( existsSync ( CUSTOM_REDIRECTS_PATH ) ) {
1617 logItem ( "# Prepending custom redirects" ) ;
17- redirects . push ( readFileSync ( CUSTOM_REDIRECTS_PATH ) ) ;
18+ redirects . push ( readFileSync ( CUSTOM_REDIRECTS_PATH , "utf8" ) ) ;
1819 }
1920
2021 // Collect redirects for NextJS pages
@@ -50,6 +51,16 @@ const setupRedirects = () => {
5051 } ) ;
5152 } ) ;
5253
54+ // This takes care of this issue: https://github.com/netlify/next-on-netlify/issues/43
55+ // where the page chunk for a root level catch-all is served incorrectly to the client.
56+ // NOTE: Netlify is also investigating this issue internally.
57+ const hasRootCatchAll = redirects . some ( isRootCatchAllRedirect ) ;
58+ if ( hasRootCatchAll ) {
59+ const rootCatchAllIndex = redirects . findIndex ( isRootCatchAllRedirect ) ;
60+ // Add general "no-op" redirect before the root catch-all redirect
61+ redirects . splice ( rootCatchAllIndex , 0 , "/_next/* /_next/:splat 200" ) ;
62+ }
63+
5364 // Write redirects to _redirects file
5465 writeFileSync ( join ( NETLIFY_PUBLISH_PATH , "_redirects" ) , redirects . join ( "\n" ) ) ;
5566} ;
0 commit comments