question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catch-all dynamic route doesn't catch a path with undefined path parameter

See original GitHub issue

Bug report

Describe the bug

Hello! I’m experiencing an issue with a dynamic catch-all route and empty path parameters. For example:

/pages/[...slug]/index.tsx

Is not catching the route: http://localhost:3000/empty//parameter

Instead, it is loading default 404 page.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

Create a simple catch-all page:

// /pages/[...slug]/index.js
export default function () {
  return <div>{'Hello!'}</div>;
}

Then try to load URL with empty path parameter:

http://localhost:3000/empty//parameter

Expected behavior

It should load the catch-all page, not a 404.

System information

  • OS: macOS
  • Browser (if applies): Chrome
  • Version of Next.js: 9.4.0
  • Version of Node.js: 14.0.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomdohnalcommented, Jun 3, 2020

This comes down to the way Next.js handles matching routes.

It uses the path-to-regexp library to create Regexes which are then matched to the routes. For the catchall route, '/:path(.*)' is passed as an argument to the pathToRegexp function.

And it behaves like this:

const regex = path.pathToRegexp("/:path*");

console.log(regex.test("/catch/all/route")); // true
console.log(regex.test("/catch//route")); // false

If you pass '/:path(.*)' instead, it can handle empty path parameters:

const regex = path.pathToRegexp("/:path(.*)");

console.log(regex.test("/catch/all/route")); // true
console.log(regex.test("/catch//route")); // true

Using '/:path*' (instead of '/:path(.*)') to match the catch-all routes is all over the Next.js codebase and I’m not sure if it’s intentional or not.

If somebody from the Next.js team could tell if it’s intentional to NOT match routes with empty parameters it’d be awesome 😃

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue Router - catch all wildcard not working - Stack Overflow
I'm trying to use dynamic route match, but when I set path: "/:user" , go to route /user1234 and try to get it...
Read more >
Dynamic Routes Details - Next.js
Here is some essential information you should know about dynamic routes. ... Dynamic routes can be extended to catch all paths by adding...
Read more >
Next.js Tutorial - 9 - Catch All Routes - YouTube
Courses - https://learn.codevolution.dev/⚡️ Checkout Taskade! https://www.taskade.com/ Support UPI - https://support.codevolution.dev/  ...
Read more >
Client-Side Routing In Next.js - Smashing Magazine
next-app └── pages ├── index.js // path: base-url (/) └── home.js ... Extending Dynamic Route Segments With Catch All Routes #.
Read more >
Implementing SSR in Next.js: Dynamic routing and prefetching
To get started, then, you should create all of your routes. ... Next.js doesn't allow you to pass undefined as a prop.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found