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

Commit ca339e4

Browse files
committed
Add routing support for catch-all routes
Create an adaptation of serverless-next.js' expressifyDynamicRoute function: getNetlifyRoute. Our getNetlifyRoute function differs from the expressifyDynamicRoute in that it converts catch-all routes from [...params] to /:* rather than to /:params*. The latter does not work with Netlify routing, but our version does.
1 parent 927eb67 commit ca339e4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/collectNextjsPages.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// - Structure is an array of objects (was an object of objects)
99

1010
const readPagesManifest = require('./readPagesManifest')
11+
const getNetlifyRoute = require('./getNetlifyRoute')
1112
const isDynamicRoute = require("./serverless-next.js/isDynamicRoute")
1213
const expressifyDynamicRoute = require("./serverless-next.js/expressifyDynamicRoute")
1314
const pathToRegexStr = require("./serverless-next.js/pathToRegexStr")
@@ -33,10 +34,14 @@ function collectNextjsPages() {
3334

3435
// Route to the page (/about)
3536
// If the page is dynamic, use url segments: /posts/[id] --> /posts/:id
36-
page.route = page.isDynamic ? expressifyDynamicRoute(route) : route
37+
page.route = page.isDynamic ? getNetlifyRoute(route) :route
3738

3839
// Regex for matching the page (/^\/about$/)
39-
page.regex = pathToRegexStr(page.route)
40+
// NOTE: This route is different than the Netlify route set above!
41+
// They are very similar, but not the same. See getNetlifyRoute.js for
42+
// more information.
43+
const _route = page.isDynamic ? expressifyDynamicRoute(route) : route
44+
page.regex = pathToRegexStr(_route)
4045

4146
return page
4247
})

lib/getNetlifyRoute.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Adapted from serverless-next.js (v1.9.10)
2+
// https://github.com/danielcondemarin/serverless-next.js/blob/master/packages/serverless-nextjs-component/lib/expressifyDynamicRoute.js
3+
// The original turns catch-all routes from /[...params] into /:params*
4+
// This adaptation turns catch-all routes from /[...params] into /:*
5+
// This is necessary for it to work with Netlify routing.
6+
7+
// converts a nextjs dynamic route /[param]/ -> /:param
8+
// also handles catch all routes /[...param]/ -> /:*
9+
module.exports = dynamicRoute => {
10+
// replace any catch all group first
11+
let expressified = dynamicRoute.replace(/\[\.\.\.(.*)]$/, "*");
12+
13+
// now replace other dynamic route groups
14+
return expressified.replace(/\[(.*?)]/g, ":$1");
15+
};

0 commit comments

Comments
 (0)