@@ -4,6 +4,7 @@ const { logTitle, logItem } = require("../helpers/logger");
4
4
const { NETLIFY_PUBLISH_PATH , CUSTOM_REDIRECTS_PATH } = require ( "../config" ) ;
5
5
const getSortedRoutes = require ( "../helpers/getSortedRoutes" ) ;
6
6
const getNetlifyRoutes = require ( "../helpers/getNetlifyRoutes" ) ;
7
+ const isRootCatchAllRedirect = require ( "../helpers/isRootCatchAllRedirect" ) ;
7
8
8
9
// Setup _redirects file that routes all requests to the appropriate location,
9
10
// such as one of the Netlify functions or one of the static files.
@@ -14,7 +15,7 @@ const setupRedirects = () => {
14
15
const redirects = [ ] ;
15
16
if ( existsSync ( CUSTOM_REDIRECTS_PATH ) ) {
16
17
logItem ( "# Prepending custom redirects" ) ;
17
- redirects . push ( readFileSync ( CUSTOM_REDIRECTS_PATH ) ) ;
18
+ redirects . push ( readFileSync ( CUSTOM_REDIRECTS_PATH , "utf8" ) ) ;
18
19
}
19
20
20
21
// Collect redirects for NextJS pages
@@ -50,6 +51,16 @@ const setupRedirects = () => {
50
51
} ) ;
51
52
} ) ;
52
53
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
+
53
64
// Write redirects to _redirects file
54
65
writeFileSync ( join ( NETLIFY_PUBLISH_PATH , "_redirects" ) , redirects . join ( "\n" ) ) ;
55
66
} ;
0 commit comments