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

Fix broken redirects (#45) #47

Merged
merged 4 commits into from
Oct 2, 2020
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
18 changes: 17 additions & 1 deletion lib/helpers/getNetlifyRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ const getNetlifyRoutes = (nextRoute) => {
// Netlify route for the base path (when no parameters are present).
// The file ending must be present!
if (nextRoute.match(OPTIONAL_CATCH_ALL_REGEX)) {
netlifyRoutes.push(nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, "$2"));
let netlifyRoute = nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, "$2");

// When optional catch-all route is at top-level, the regex on line 19 will
// create an empty string, but actually needs to be a forward slash
if (netlifyRoute === "") netlifyRoute = "/";

// When optional catch-all route is at top-level, the regex on line 19 will
// create an incorrect route for the data route. For example, it creates
// /_next/data/%BUILDID%.json, but NextJS looks for
// /_next/data/%BUILDID%/index.json
netlifyRoute = netlifyRoute.replace(
/(\/_next\/data\/[^/]+).json/,
"$1/index.json"
);

// Add second route to the front of the array
netlifyRoutes.unshift(netlifyRoute);
}

// Replace catch-all, e.g., [...slug]
Expand Down
8 changes: 4 additions & 4 deletions lib/helpers/getSortedRoutes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {
getSortedRoutes: getSortedRoutesFromSlsNext,
} = require("@sls-next/lambda-at-edge/dist/lib/sortedRoutes");
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-z]+$/, "");
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
Expand All @@ -16,7 +16,7 @@ const getSortedRoutes = (routes) => {
);

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

// Return original routes in the sorted order
return routes.sort(
Expand Down
2 changes: 0 additions & 2 deletions lib/pages/getStaticProps/redirects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { relative } = require("path");
const isDynamicRoute = require("../../helpers/isDynamicRoute");
const pages = require("./pages");

// Pages with getStaticProps do not need redirects, unless they are using
Expand Down
10 changes: 5 additions & 5 deletions tests/__snapshots__/defaults.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
exports[`Routing creates Netlify redirects 1`] = `
"# Next-on-Netlify Redirects
/ /.netlify/functions/next_index 200
/_next/data/%BUILD_ID%/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/_next/data/%BUILD_ID%/getServerSideProps/all.json /.netlify/functions/next_getServerSideProps_all_slug 200
/_next/data/%BUILD_ID%/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
/_next/data/%BUILD_ID%/getServerSideProps/:id.json /.netlify/functions/next_getServerSideProps_id 200
/_next/data/%BUILD_ID%/getStaticProps/with-revalidate.json /.netlify/functions/next_getStaticProps_withrevalidate 200
Expand All @@ -13,20 +13,20 @@ 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
/api/shows/:params/* /.netlify/functions/next_api_shows_params 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/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/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withRevalidate/1 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/getStaticProps/withRevalidate/2 /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/shows/:params/* /.netlify/functions/next_shows_params 200
/shows/:id /.netlify/functions/next_shows_id 200
/shows/:params/* /.netlify/functions/next_shows_params 200
/static/:id /static/[id].html 200"
`;
10 changes: 6 additions & 4 deletions tests/__snapshots__/optionalCatchAll.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

exports[`Routing creates Netlify redirects 1`] = `
"# Next-on-Netlify Redirects
/_next/data/%BUILD_ID%/catch/* /.netlify/functions/next_catch_all 200
/_next/data/%BUILD_ID%/catch.json /.netlify/functions/next_catch_all 200
/catch/* /.netlify/functions/next_catch_all 200
/catch /.netlify/functions/next_catch_all 200"
/_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
/page /.netlify/functions/next_page 200
/ /.netlify/functions/next_all 200
/* /.netlify/functions/next_all 200"
`;
6 changes: 0 additions & 6 deletions tests/fixtures/next.config.js-with-optionalCatchAll

This file was deleted.

25 changes: 0 additions & 25 deletions tests/fixtures/pages-with-optionalCatchAll/index.js

This file was deleted.

36 changes: 36 additions & 0 deletions tests/fixtures/pages-with-optionalCatchAll/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Link from "next/link";

const Index = () => (
<div>
<ul>
<li>
<Link href="/">
<a>/</a>
</Link>
</li>
<li>
<Link href="/catch/25/catch/all">
<a>/catch/25/catch/all</a>
</Link>
</li>
<li>
<Link href="/catch/75/undefined/path/test">
<a>/catch/75/undefined/path/test</a>
</Link>
</li>
</ul>
</div>
);

export const getServerSideProps = async ({ params }) => {
const res = await fetch("https://api.tvmaze.com/shows/42");
const data = await res.json();

return {
props: {
show: data,
},
};
};

export default Index;
2 changes: 1 addition & 1 deletion tests/optionalCatchAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ beforeAll(
buildOutput = await buildNextApp()
.forTest(__filename)
.withPages("pages-with-optionalCatchAll")
.withNextConfig("next.config.js-with-optionalCatchAll")
.withNextConfig("next.config.js")
.withPackageJson("package.json")
.build();
},
Expand Down