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

Commit 0c6ab75

Browse files
committed
fix: page chunk for root level catch-all is served incorrectly to client
1 parent ed3b50f commit 0c6ab75

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

lib/constants/regex.js

Lines changed: 9 additions & 0 deletions
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

Lines changed: 5 additions & 3 deletions
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/isCatchAllRoute.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const {
2+
CATCH_ALL_REGEX,
3+
OPTIONAL_CATCH_ALL_REGEX,
4+
} = require("../constants/regex");
5+
6+
// Return true if the route is a catch-all or optional catch-all
7+
// (e.g., /[...slug] or [[...slug]])
8+
const isCatchAllRoute = (route) => {
9+
return route.match(OPTIONAL_CATCH_ALL_REGEX) || route.match(CATCH_ALL_REGEX);
10+
};
11+
12+
module.exports = isCatchAllRoute;

lib/steps/setupRedirects.js

Lines changed: 12 additions & 0 deletions
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 isCatchAllRoute = require("../helpers/isCatchAllRoute");
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.
@@ -35,12 +36,17 @@ const setupRedirects = () => {
3536
// less-specific routes (e.g., catch-all)
3637
const sortedRoutes = getSortedRoutes(nextRedirects.map(({ route }) => route));
3738

39+
// Create variable for tracking if there is a catch-all route
40+
let hasCatchAllRoute = false;
41+
3842
// Assemble redirects for each route
3943
sortedRoutes.forEach((route) => {
4044
const nextRedirect = nextRedirects.find(
4145
(redirect) => redirect.route === route
4246
);
4347

48+
if (isCatchAllRoute(route)) hasCatchAllRoute = true;
49+
4450
// One route may map to multiple Netlify routes: e.g., catch-all pages
4551
// require two Netlify routes in the _redirects file
4652
getNetlifyRoutes(route).map((netlifyRoute) => {
@@ -50,6 +56,12 @@ const setupRedirects = () => {
5056
});
5157
});
5258

59+
// This redirect takes care of this issue: https://github.com/netlify/next-on-netlify/issues/43
60+
// where the page chunk for a root level catch-all is served incorrectly to the client.
61+
// Add last because it's least specific.
62+
if (nextRedirects.length >= 1 && hasCatchAllRoute)
63+
redirects.push("/_next/* /_next/static/* 200");
64+
5365
// Write redirects to _redirects file
5466
writeFileSync(join(NETLIFY_PUBLISH_PATH, "_redirects"), redirects.join("\n"));
5567
};

tests/__snapshots__/defaults.test.js.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ exports[`Routing creates Netlify redirects 1`] = `
2828
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
2929
/shows/:id /.netlify/functions/next_shows_id 200
3030
/shows/:params/* /.netlify/functions/next_shows_params 200
31-
/static/:id /static/[id].html 200"
31+
/static/:id /static/[id].html 200
32+
/_next/* /_next/static/* 200"
3233
`;

tests/__snapshots__/optionalCatchAll.test.js.snap

Lines changed: 2 additions & 1 deletion
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-
/* /.netlify/functions/next_all 200"
10+
/* /.netlify/functions/next_all 200
11+
/_next/* /_next/static/* 200"
1112
`;

0 commit comments

Comments
 (0)