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

Commit b1efd12

Browse files
committed
Refactor: Split allNextJsPages.js into pages/ folder
The allNextJsPages.js file has become too long and difficult to read. Instead, add a pages/ folder with seven subdirectories, one for each page type of NextJS: - api - getInitialProps - getServerSideProps - getStaticProps - getStaticPropsWithFallback - getStaticPropsWithRevalidate - withoutProps Each folder contains a pages.js file that collects the relevant pages, a setup.js file that sets up the Netlify function for the page or copies the pre-rendered files to the correct locations, and a redirects.js file that returns the list of redirects/rewrites to set up for these pages. We now have a lot more files than before, but it should be much easier to understand than the prior allNextJsPages.js file.
1 parent 802d3eb commit b1efd12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+845
-473
lines changed

lib/allNextJsPages.js

-189
This file was deleted.

lib/helpers/getDataRouteForRoute.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { join } = require("path");
2+
const { readFileSync } = require("fs-extra");
3+
const { NEXT_DIST_DIR } = require("../config");
4+
const getFilePathForRoute = require("./getFilePathForRoute");
5+
6+
// Get build ID that is used for data routes, e.g. /_next/data/BUILD_ID/...
7+
const fileContents = readFileSync(join(NEXT_DIST_DIR, "BUILD_ID"));
8+
const buildId = fileContents.toString();
9+
10+
// Return the data route for the given route
11+
const getDataRouteForRoute = (route) => {
12+
const filePath = getFilePathForRoute(route, "json");
13+
14+
return join("/_next", "data", buildId, filePath);
15+
};
16+
17+
module.exports = getDataRouteForRoute;

lib/helpers/getFilePathForRoute.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Return the file for the given route
2+
const getFilePathForRoute = (route, extension) => {
3+
// Replace / with /index
4+
const path = route.replace(/^\/$/, "/index");
5+
6+
return `${path}.${extension}`;
7+
};
8+
9+
module.exports = getFilePathForRoute;

lib/helpers/getNetlifyRoute.js

-24
This file was deleted.

lib/helpers/getNetlifyRoutes.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Adapted from @sls-next/lambda-at-edge (v1.2.0-alpha.3)
2+
// See: https://github.com/danielcondemarin/serverless-next.js/blob/57142970b08e6bc3faf0fc70749b3b0501ad7869/packages/lambda-at-edge/src/lib/expressifyDynamicRoute.ts#L4
3+
4+
const CATCH_ALL_REGEX = /\/\[\.{3}(.*)\](.json)?$/;
5+
const OPTIONAL_CATCH_ALL_REGEX = /\/\[{2}\.{3}(.*)\]{2}(.json)?$/;
6+
const DYNAMIC_PARAMETER_REGEX = /\/\[(.*?)\]/g;
7+
8+
// Convert dynamic NextJS routes into their Netlify-equivalent
9+
// Note that file endings (.json) must be removed for catch-all and optional
10+
// catch-all routes to work. That's why the regexes defined above include
11+
// the (.json)? option
12+
const getNetlifyRoutes = (nextRoute) => {
13+
let netlifyRoutes = [nextRoute];
14+
15+
// If the route is an optional catch-all route, we need to add a second
16+
// Netlify route for the base path (when no parameters are present).
17+
// The file ending must be present!
18+
if (nextRoute.match(OPTIONAL_CATCH_ALL_REGEX)) {
19+
netlifyRoutes.push(nextRoute.replace(OPTIONAL_CATCH_ALL_REGEX, "$2"));
20+
}
21+
22+
// Replace catch-all, e.g., [...slug]
23+
netlifyRoutes = netlifyRoutes.map((route) =>
24+
route.replace(CATCH_ALL_REGEX, "/:$1/*")
25+
);
26+
27+
// Replace optional catch-all, e.g., [[...slug]]
28+
netlifyRoutes = netlifyRoutes.map((route) =>
29+
route.replace(OPTIONAL_CATCH_ALL_REGEX, "/*")
30+
);
31+
32+
// Replace dynamic parameters, e.g., [id]
33+
netlifyRoutes = netlifyRoutes.map((route) =>
34+
route.replace(DYNAMIC_PARAMETER_REGEX, "/:$1")
35+
);
36+
37+
return netlifyRoutes;
38+
};
39+
40+
module.exports = getNetlifyRoutes;

lib/helpers/getPagesManifest.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { join } = require("path");
2+
const { readJSONSync } = require("fs-extra");
3+
const { NEXT_DIST_DIR } = require("../config");
4+
5+
const getPagesManifest = () => {
6+
return readJSONSync(join(NEXT_DIST_DIR, "serverless", "pages-manifest.json"));
7+
};
8+
9+
module.exports = getPagesManifest;

lib/helpers/getPrerenderManifest.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { join } = require("path");
2+
const { readJSONSync } = require("fs-extra");
3+
const { NEXT_DIST_DIR } = require("../config");
4+
5+
const getPrerenderManifest = () => {
6+
return readJSONSync(join(NEXT_DIST_DIR, "prerender-manifest.json"));
7+
};
8+
9+
module.exports = getPrerenderManifest;

lib/helpers/getRoutesManifest.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { join } = require("path");
2+
const { readJSONSync } = require("fs-extra");
3+
const { NEXT_DIST_DIR } = require("../config");
4+
5+
const getRoutesManifest = () => {
6+
return readJSONSync(join(NEXT_DIST_DIR, "routes-manifest.json"));
7+
};
8+
9+
module.exports = getRoutesManifest;

lib/helpers/getSortedRoutes.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const {
2+
getSortedRoutes: getSortedRoutesFromSlsNext,
3+
} = require("@sls-next/lambda-at-edge/dist/lib/sortedRoutes");
4+
5+
// Remove the file extension form the route
6+
const removeFileExtension = (route) => route.replace(/\.[A-z]+$/, "");
7+
8+
// Return an array of routes sorted in order of specificity, i.e., more generic
9+
// routes precede more specific ones
10+
const getSortedRoutes = (routes) => {
11+
// The @sls-next getSortedRoutes does not correctly sort routes with file
12+
// endings (e.g., json), so we remove them before sorting and add them back
13+
// after sorting
14+
const routesWithoutExtensions = routes.map((route) =>
15+
removeFileExtension(route)
16+
);
17+
18+
// Sort the "naked" routes
19+
const sortedRoutes = getSortedRoutesFromSlsNext(routesWithoutExtensions);
20+
21+
// Return original routes in the sorted order
22+
return routes.sort(
23+
(routeA, routeB) =>
24+
sortedRoutes.indexOf(removeFileExtension(routeA)) -
25+
sortedRoutes.indexOf(removeFileExtension(routeB))
26+
);
27+
};
28+
29+
module.exports = getSortedRoutes;

lib/helpers/isApiRoute.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Return true if the route is an API route
2+
const isApiRoute = (route) => {
3+
return route.startsWith("/api/");
4+
};
5+
6+
module.exports = isApiRoute;

lib/helpers/isDynamicRoute.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Return true if the route uses dynamic routing (e.g., [id] or [...slug])
2+
const {
3+
default: isDynamicRoute,
4+
} = require("@sls-next/lambda-at-edge/dist/lib/isDynamicRoute");
5+
6+
module.exports = isDynamicRoute;

lib/helpers/isFrameworkRoute.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Return true if the route is one of the framework pages, such as _app or
2+
// _error
3+
const isFrameworkRoute = (route) => {
4+
return ["/_app", "/_document", "/_error"].includes(route);
5+
};
6+
7+
module.exports = isFrameworkRoute;

lib/helpers/isHtmlFile.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Return true if the file path is an HTML file
2+
const isHtmlFile = (filePath) => {
3+
return filePath.endsWith(".html");
4+
};
5+
6+
module.exports = isHtmlFile;
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const getPrerenderManifest = require("./getPrerenderManifest");
2+
3+
const { routes, dynamicRoutes } = getPrerenderManifest();
4+
5+
// Return true if the route is defined in the prerender manifest
6+
const isRouteInPrerenderManifest = (route) => {
7+
return route in routes || route in dynamicRoutes;
8+
};
9+
10+
module.exports = isRouteInPrerenderManifest;

lib/helpers/isRouteWithDataRoute.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const getRoutesManifest = require("./getRoutesManifest");
2+
3+
const { dataRoutes } = getRoutesManifest();
4+
5+
// Return true if the route has a data route in the routes manifest
6+
const isRouteInPrerenderManifest = (route) => {
7+
// If no data routes exist, return false
8+
if (dataRoutes == null) return false;
9+
10+
return dataRoutes.find((dataRoute) => dataRoute.page === route);
11+
};
12+
13+
module.exports = isRouteInPrerenderManifest;

0 commit comments

Comments
 (0)