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 routes do not catch the root index route

See original GitHub issue

Feature Request

Describe the use case

I have a use case where I need all of the following requests to go to the same function:

POST pages/api/users
PATCH pages/api/users/1
DELETE pages/api/users/1

After reading the docs, I was sure I could catch all of those using this:

pages/api/users/[...slug].js

However, this does not catch POST pages/api/users without an explicit /index.js.

To Reproduce

Try the above scenario (even with GET requests)

Expected behavior

This route:

pages/api/users/[...slug].js

Catches all of these:

POST pages/api/users
PATCH pages/api/users/1
DELETE pages/api/users/1

Screenshots

N/A

System information

N/A

Additional context

Next.js 9.2.2-canary.15

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:48
  • Comments:38 (25 by maintainers)

github_iconTop GitHub Comments

37reactions
Janpotcommented, Feb 12, 2020

@timneutkens created https://github.com/zeit/next.js/pull/10502

Been thinking a bit more about this issue and I think the requested behavior could make more sense for the following reasons:

  1. Conceptually [...slug] looks like array destructuring into an array. Arrays can have zero items, so why can’t slug have zero items? The index route would just receive an empty array as the slug parameter.

  2. The solution I proposed is flawed. Let me give an example:

Current situation, imagine we have a catch-all route like:

// /api/posts/[...slug].js
export default function (req, res) {
  res.send(getPost(req.query.slug))
}

And we fetch the following urls:

url getPost called with
/api/posts 404 wtf? /api/posts/index resolves, so why not this?
/api/posts/index ['index'] ok
/api/posts/something ['something'] ok
/api/posts/something/else ['something', 'else'] ok

Imagine we add a index route:

// /api/posts/index.js
export { default } from './[...slug]';

now this behavior becomes

url getPost called with
/api/posts undefined ok (I guess)
/api/posts/index undefined wtf?
/api/posts/something ['something'] ok
/api/posts/something/else ['something', 'else'] ok

Now, imagine /api/posts/[...slug].js would also be called for the index route, like this issue proposes. Then with just the following route

// /api/posts/[...slug].js
export default function (req, res) {
  res.send(getPost(req.query.slug))
}

the behavior would be more predictable and uniform and zen

url getPost called with
/api/posts [] ok
/api/posts/index ['index'] ok
/api/posts/something ['something'] ok
/api/posts/something/else ['something', 'else'] ok
28reactions
timneutkenscommented, Feb 11, 2020

I’ll keep the issue open to see if there’s anyone else asking for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring catch all route for index.html - Stack Overflow
I'm developing a spring backend for a react-based single page application where I'm using react-router for client-side routing. Beside the index.html page the ......
Read more >
ASP.NET Core Blazor routing and navigation - Microsoft Learn
Catch-all route parameters, which capture paths across multiple folder boundaries, are supported in components. Catch-all route parameters are:.
Read more >
Next.js Optional catch all routes - GeeksforGeeks
In this article, we will learn how we can optional catch-all routes in our NextJS project. NextJS is a React-based framework.
Read more >
Working with routes for HTTP APIs - Amazon API Gateway
You can create a $default route that acts as a catch-all for requests that don't match any other routes. Working with path variables....
Read more >
Routing • Docs • SvelteKit
src/routes is the root route; src/routes/about creates an /about route ... +error.svelte is not used when an error occurs inside handle or a...
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