From a765bdfcbc52bca74f4e5d3d7f5cca772cb56de7 Mon Sep 17 00:00:00 2001 From: Lindsay Levine Date: Sun, 17 Jan 2021 07:03:23 -0500 Subject: [PATCH 1/2] revert route/redirect sorting logic to static then dynamic --- lib/steps/setupRedirects.js | 82 ++++++++++--------- tests/__snapshots__/defaults.test.js.snap | 18 ++-- tests/__snapshots__/i18n.test.js.snap | 60 +++++++------- .../optionalCatchAll.test.js.snap | 2 +- tests/staticIndexPages.test.js | 3 +- 5 files changed, 84 insertions(+), 81 deletions(-) diff --git a/lib/steps/setupRedirects.js b/lib/steps/setupRedirects.js index 7853435..50f2dab 100644 --- a/lib/steps/setupRedirects.js +++ b/lib/steps/setupRedirects.js @@ -8,6 +8,7 @@ const { const getSortedRoutes = require("../helpers/getSortedRoutes"); 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. @@ -32,53 +33,56 @@ 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; - }); + // Sort routes: More-specific routes precede less-specific routes (e.g., catch-all) + const getSortedRedirects = (redirects) => { + const sortedRoutes = getSortedRoutes(redirects.map(({ route }) => route)); + return redirects.sort( + (a, b) => sortedRoutes.indexOf(a.route) - sortedRoutes.indexOf(b.route) + ); }; + 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. diff --git a/tests/__snapshots__/defaults.test.js.snap b/tests/__snapshots__/defaults.test.js.snap index 65b473f..01c8d7e 100644 --- a/tests/__snapshots__/defaults.test.js.snap +++ b/tests/__snapshots__/defaults.test.js.snap @@ -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 @@ -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 diff --git a/tests/__snapshots__/i18n.test.js.snap b/tests/__snapshots__/i18n.test.js.snap index 67e5218..52b9086 100644 --- a/tests/__snapshots__/i18n.test.js.snap +++ b/tests/__snapshots__/i18n.test.js.snap @@ -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 @@ -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 @@ -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 +/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" `; diff --git a/tests/__snapshots__/optionalCatchAll.test.js.snap b/tests/__snapshots__/optionalCatchAll.test.js.snap index 3c2534b..095bb51 100644 --- a/tests/__snapshots__/optionalCatchAll.test.js.snap +++ b/tests/__snapshots__/optionalCatchAll.test.js.snap @@ -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" diff --git a/tests/staticIndexPages.test.js b/tests/staticIndexPages.test.js index cb58a46..fcc6723 100644 --- a/tests/staticIndexPages.test.js +++ b/tests/staticIndexPages.test.js @@ -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" ); }); From f83cb310fb79925cd619d00a66bcdebce53e4240 Mon Sep 17 00:00:00 2001 From: Finn Woelm Date: Sun, 17 Jan 2021 21:06:55 +0100 Subject: [PATCH 2/2] Force redirects with conditions before plain redirects --- lib/helpers/getSortedRedirects.js | 35 +++++++++++++++++++++++++++++++ lib/helpers/getSortedRoutes.js | 29 ------------------------- lib/steps/setupRedirects.js | 9 +------- 3 files changed, 36 insertions(+), 37 deletions(-) create mode 100644 lib/helpers/getSortedRedirects.js delete mode 100644 lib/helpers/getSortedRoutes.js diff --git a/lib/helpers/getSortedRedirects.js b/lib/helpers/getSortedRedirects.js new file mode 100644 index 0000000..b31b016 --- /dev/null +++ b/lib/helpers/getSortedRedirects.js @@ -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; diff --git a/lib/helpers/getSortedRoutes.js b/lib/helpers/getSortedRoutes.js deleted file mode 100644 index 50b09f8..0000000 --- a/lib/helpers/getSortedRoutes.js +++ /dev/null @@ -1,29 +0,0 @@ -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 routes sorted in order of specificity, i.e., more generic -// routes precede more specific ones -const getSortedRoutes = (routes) => { - // 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 = routes.map((route) => - removeFileExtension(route) - ); - - // Sort the "naked" routes - const sortedRoutes = getSortedRoutesFromNext(routesWithoutExtensions); - - // Return original routes in the sorted order - return routes.sort( - (routeA, routeB) => - sortedRoutes.indexOf(removeFileExtension(routeA)) - - sortedRoutes.indexOf(removeFileExtension(routeB)) - ); -}; - -module.exports = getSortedRoutes; diff --git a/lib/steps/setupRedirects.js b/lib/steps/setupRedirects.js index 50f2dab..355b7a2 100644 --- a/lib/steps/setupRedirects.js +++ b/lib/steps/setupRedirects.js @@ -5,7 +5,7 @@ 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"); @@ -49,13 +49,6 @@ const setupRedirects = (publishPath) => { target: `/.netlify/functions/${NEXT_IMAGE_FUNCTION_NAME}?url=:url&w=:width&q=:quality`, }); - // Sort routes: More-specific routes precede less-specific routes (e.g., catch-all) - const getSortedRedirects = (redirects) => { - const sortedRoutes = getSortedRoutes(redirects.map(({ route }) => route)); - return redirects.sort( - (a, b) => sortedRoutes.indexOf(a.route) - sortedRoutes.indexOf(b.route) - ); - }; const sortedStaticRedirects = getSortedRedirects(staticRedirects); const sortedDynamicRedirects = getSortedRedirects(dynamicRedirects);