Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 954c4a8

Browse files
fix: page chunk for root level catch-all is served incorrectly to client (#52)
1 parent aa032e8 commit 954c4a8

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

lib/constants/regex.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)\](.json)?$/;
2+
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)\]{2}(.json)?$/;
3+
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)\]/g;
4+
5+
module.exports = {
6+
CATCH_ALL_REGEX,
7+
OPTIONAL_CATCH_ALL_REGEX,
8+
DYNAMIC_PARAMETER_REGEX,
9+
};

lib/helpers/getNetlifyRoutes.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Adapted from @sls-next/lambda-at-edge (v1.2.0-alpha.3)
22
// See: https://github.com/danielcondemarin/serverless-next.js/blob/57142970b08e6bc3faf0fc70749b3b0501ad7869/packages/lambda-at-edge/src/lib/expressifyDynamicRoute.ts#L4
33

4-
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)\](.json)?$/;
5-
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)\]{2}(.json)?$/;
6-
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)\]/g;
4+
const {
5+
CATCH_ALL_REGEX,
6+
OPTIONAL_CATCH_ALL_REGEX,
7+
DYNAMIC_PARAMETER_REGEX,
8+
} = require("../constants/regex");
79

810
// Convert dynamic NextJS routes into their Netlify-equivalent
911
// Note that file endings (.json) must be removed for catch-all and optional

lib/helpers/isRootCatchAllRedirect.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Return true if the redirect is a root level catch-all
2+
// (e.g., /[[...slug]])
3+
const isRootCatchAllRedirect = (redirect) => redirect.startsWith("/*");
4+
5+
module.exports = isRootCatchAllRedirect;

lib/steps/setupRedirects.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { logTitle, logItem } = require("../helpers/logger");
44
const { NETLIFY_PUBLISH_PATH, CUSTOM_REDIRECTS_PATH } = require("../config");
55
const getSortedRoutes = require("../helpers/getSortedRoutes");
66
const 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
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"husky": {
5858
"hooks": {
59-
"pre-commit": "npm run format"
59+
"pre-commit": "prettier --check ."
6060
}
6161
}
6262
}

tests/__snapshots__/optionalCatchAll.test.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ exports[`Routing creates Netlify redirects 1`] = `
77
/_next/data/%BUILD_ID%/* /.netlify/functions/next_all 200
88
/page /.netlify/functions/next_page 200
99
/ /.netlify/functions/next_all 200
10+
/_next/* /_next/:splat 200
1011
/* /.netlify/functions/next_all 200"
1112
`;

0 commit comments

Comments
 (0)