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

revert route/redirect sorting logic to static then dynamic #145

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/helpers/getSortedRedirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {
getSortedRoutes: getSortedRoutesFromNext,
} = require("next/dist/next-server/lib/router/utils/sorted-routes");

// Remove the file extension form the route
const removeFileExtension = (route) => route.replace(/\.[a-zA-Z]+$/, "");

// Return an array of redirects sorted in order of specificity, i.e., more generic
// routes precede more specific ones
const getSortedRedirects = (redirects) => {
// The @sls-next getSortedRoutes does not correctly sort routes with file
// endings (e.g., json), so we remove them before sorting and add them back
// after sorting
const routesWithoutExtensions = redirects.map(({ route }) =>
removeFileExtension(route)
);

// Sort the "naked" routes
const sortedRoutes = getSortedRoutesFromNext(routesWithoutExtensions);

// Return original routes in the sorted order
return redirects.sort((a, b) => {
// If routes are different, sort according to Next.js' getSortedRoutes
if (a.route !== b.route) {
return (
sortedRoutes.indexOf(removeFileExtension(a.route)) -
sortedRoutes.indexOf(removeFileExtension(b.route))
);
}
// Otherwise, put the route with more conditions first
return (b.conditions || []).length - (a.conditions || []).length;
});
};

module.exports = getSortedRedirects;
29 changes: 0 additions & 29 deletions lib/helpers/getSortedRoutes.js

This file was deleted.

79 changes: 38 additions & 41 deletions lib/steps/setupRedirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const {
CUSTOM_REDIRECTS_PATH,
NEXT_IMAGE_FUNCTION_NAME,
} = require("../config");
const getSortedRoutes = require("../helpers/getSortedRoutes");
const getSortedRedirects = require("../helpers/getSortedRedirects");
const getNetlifyRoutes = require("../helpers/getNetlifyRoutes");
const isRootCatchAllRedirect = require("../helpers/isRootCatchAllRedirect");
const isDynamicRoute = require("../helpers/isDynamicRoute");

// Setup _redirects file that routes all requests to the appropriate location,
// such as one of the Netlify functions or one of the static files.
Expand All @@ -32,53 +33,49 @@ const setupRedirects = (publishPath) => {
...require("../pages/withoutProps/redirects"),
];

// Add _redirect section heading
redirects.push("# Next-on-Netlify Redirects");

const staticRedirects = nextRedirects.filter(
({ route }) => !isDynamicRoute(route)
);
const dynamicRedirects = nextRedirects.filter(({ route }) =>
isDynamicRoute(route)
);

// Add next/image redirect to our image function
nextRedirects.push({
dynamicRedirects.push({
route: "/_next/image* url=:url w=:width q=:quality",
target: `/.netlify/functions/${NEXT_IMAGE_FUNCTION_NAME}?url=:url&w=:width&q=:quality`,
});

// Add _redirect section heading
redirects.push("# Next-on-Netlify Redirects");

// Sort routes: More-specific routes (e.g., static routing) precede
// less-specific routes (e.g., catch-all)
const sortedRoutes = getSortedRoutes(nextRedirects.map(({ route }) => route));

// There may be several redirects with the same route but different targets
const wasRedirectAdded = (redirect) => {
return redirects.find((addedRedirect) => {
const [route, target] = addedRedirect.split(" ");
return redirect.route === route && redirect.target === target;
});
};
const sortedStaticRedirects = getSortedRedirects(staticRedirects);
const sortedDynamicRedirects = getSortedRedirects(dynamicRedirects);

// Assemble redirects for each route
sortedRoutes.forEach((route) => {
const nextRedirect = nextRedirects.find(
(redirect) => redirect.route === route && !wasRedirectAdded(redirect)
);

// One route may map to multiple Netlify routes: e.g., catch-all pages
// require two Netlify routes in the _redirects file
getNetlifyRoutes(route).map((netlifyRoute) => {
const {
conditions = [],
force = false,
statusCode = "200",
target,
} = nextRedirect;
const redirectPieces = [
netlifyRoute,
target,
`${statusCode}${force ? "!" : ""}`,
conditions.join(" "),
];
const redirect = redirectPieces.join(" ").trim();
logItem(redirect);
redirects.push(redirect);
});
});
[...sortedStaticRedirects, ...sortedDynamicRedirects].forEach(
(nextRedirect) => {
// One route may map to multiple Netlify routes: e.g., catch-all pages
// require two Netlify routes in the _redirects file
getNetlifyRoutes(nextRedirect.route).map((netlifyRoute) => {
const {
conditions = [],
force = false,
statusCode = "200",
target,
} = nextRedirect;
const redirectPieces = [
netlifyRoute,
target,
`${statusCode}${force ? "!" : ""}`,
conditions.join(" "),
];
const redirect = redirectPieces.join(" ").trim();
logItem(redirect);
redirects.push(redirect);
});
}
);

// This takes care of this issue: https://github.com/netlify/next-on-netlify/issues/43
// where the page chunk for a root level catch-all is served incorrectly to the client.
Expand Down
18 changes: 9 additions & 9 deletions tests/__snapshots__/defaults.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ exports[`Routing creates Netlify redirects 1`] = `
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/1.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/2.json /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/withFallback/:id.json /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/api/shows/:id /.netlify/functions/next_api_shows_id 200
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
/api/static /.netlify/functions/next_api_static 200
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
Expand All @@ -47,13 +41,19 @@ exports[`Routing creates Netlify redirects 1`] = `
/getStaticProps/withFallback/4 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/my/path/1 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withFallbackBlocking/3 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallbackBlocking/4 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/api/shows/:id /.netlify/functions/next_api_shows_id 200
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/shows/:id /.netlify/functions/next_shows_id 200
/shows/:params/* /.netlify/functions/next_shows_params 200
Expand Down
60 changes: 30 additions & 30 deletions tests/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ exports[`Routing creates Netlify redirects 1`] = `
/_next/data/%BUILD_ID%/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/_next/data/%BUILD_ID%/getStaticProps/withFallbackBlocking/:id.json /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/_next/data/%BUILD_ID%/getStaticProps/withRevalidate/withFallback/:id.json /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/api/shows/:id /.netlify/functions/next_api_shows_id 200
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
/api/static /.netlify/functions/next_api_static 200
/en /.netlify/functions/next_index 200
/en/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/en/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/en/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/en/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/en/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
Expand All @@ -65,35 +59,15 @@ exports[`Routing creates Netlify redirects 1`] = `
/en/getStaticProps/withFallback/4 /.netlify/functions/next_getStaticProps_withFallback_id 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/withFallback/my/path/1 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/en/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/en/getStaticProps/withFallbackBlocking/3 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/withFallbackBlocking/4 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/en/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/en/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/en/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/en/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/en/shows/:id /.netlify/functions/next_shows_id 200
/en/shows/:params/* /.netlify/functions/next_shows_params 200
/en/static/:id /en/static/[id].html 200
/es /.netlify/functions/next_index 200
/es/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/es/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/es/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/es/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/es/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200! Cookie=__prerender_bypass,__next_preview_data
/es/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
/es/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/es/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/es/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/es/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/es/shows/:id /.netlify/functions/next_shows_id 200
/es/shows/:params/* /.netlify/functions/next_shows_params 200
/es/static/:id /es/static/[id].html 200
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/1 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/1 /en/getStaticProps/1 200
/getStaticProps/2 /.netlify/functions/next_getStaticProps_id 200! Cookie=__prerender_bypass,__next_preview_data
Expand All @@ -109,18 +83,44 @@ exports[`Routing creates Netlify redirects 1`] = `
/getStaticProps/withFallback/my/path/1 /en/getStaticProps/withFallback/my/path/1 200
/getStaticProps/withFallback/my/path/2 /.netlify/functions/next_getStaticProps_withFallback_slug 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallback/my/path/2 /en/getStaticProps/withFallback/my/path/2 200
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withFallbackBlocking/3 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallbackBlocking/3 /en/getStaticProps/withFallbackBlocking/3 200
/getStaticProps/withFallbackBlocking/4 /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200! Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/withFallbackBlocking/4 /en/getStaticProps/withFallbackBlocking/4 200
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/static /en/static.html 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/api/shows/:id /.netlify/functions/next_api_shows_id 200
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
/en/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/en/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/en/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/en/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/en/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/en/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/en/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/en/shows/:id /.netlify/functions/next_shows_id 200
/en/shows/:params/* /.netlify/functions/next_shows_params 200
/en/static/:id /en/static/[id].html 200
/es/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/es/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/es/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/es/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/es/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/es/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/es/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/es/shows/:id /.netlify/functions/next_shows_id 200
/es/shows/:params/* /.netlify/functions/next_shows_params 200
/es/static/:id /es/static/[id].html 200
/getServerSideProps/all /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
FinnWoelm marked this conversation as resolved.
Show resolved Hide resolved
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withFallbackBlocking/:id /.netlify/functions/next_getStaticProps_withFallbackBlocking_id 200
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/shows/:id /.netlify/functions/next_shows_id 200
/shows/:params/* /.netlify/functions/next_shows_params 200
/static /en/static.html 200
/static/:id /en/static/[id].html 200"
`;
2 changes: 1 addition & 1 deletion tests/__snapshots__/optionalCatchAll.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ exports[`Routing creates Netlify redirects 1`] = `
/_next/data/%BUILD_ID%/page.json /.netlify/functions/next_page 200
/_next/data/%BUILD_ID%/index.json /.netlify/functions/next_all 200
/_next/data/%BUILD_ID%/* /.netlify/functions/next_all 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/page /.netlify/functions/next_page 200
/_next/image* url=:url w=:width q=:quality /.netlify/functions/next_image?url=:url&w=:width&q=:quality 200
/ /.netlify/functions/next_all 200
/_next/* /_next/:splat 200
/* /.netlify/functions/next_all 200"
Expand Down
3 changes: 1 addition & 2 deletions tests/staticIndexPages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ describe("Routing", () => {
expect(redirects[3]).toEqual(
"/_next/data/%BUILD_ID%/static.json /.netlify/functions/next_static 200! Cookie=__prerender_bypass,__next_preview_data"
);
// [4] is the next_image redirect
expect(redirects[5]).toEqual(
expect(redirects[4]).toEqual(
"/static /.netlify/functions/next_static 200! Cookie=__prerender_bypass,__next_preview_data"
);
});
Expand Down