next.js: Catch-all routes do not catch the root index route

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

About this issue

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

Commits related to this issue

Most upvoted comments

@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

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

Now that blitz is in such high demand, could this feature be approved?

Below are the files for my tiny blog demo. The lack of this feature literally doubles the files needed. All of the index files are just re-exports of [...id]

Currently:

pages/api/posts/[...id].js
pages/api/posts/index.js
pages/api/posts/[postId]/comments/[...id].js
pages/api/posts/[postId]/comments/index.js
pages/api/comments/[...id].js
pages/api/comments/index.js

After this feature:

pages/api/posts/[...id].js
pages/api/posts/[postId]/comments/[...id].js
pages/api/comments/[...id].js

The description by @Janpot is exactly the behaviour I expected and have been implementing with a now.json rewrite rule that weirdly only works on Now (not with now dev). My use case is to capture all routes (including NO route) and pass them through to an headless CMS.

EDIT: My new workaround is just to have an additional index.tsx with the following:

import Page from './[...slug]'

export default Page

This works with now dev.

Concretely, making a page named: pages/hello/[[...world]].js would match /hello, /hello/1, and /hello/2/3, etc.

Today, making a page named: pages/hello/[...world].js only matches /hello/1, and /hello/2/3, etc, but not /hello.


We’re happy to take a PR if anyone wants to work on this.

I would also benefit from this feature. Right now another option seems to be implementing a custom routing which I think is overcomplicated for this case.

@timneutkens What about having another operator before the slug, something like pages/~[…slug].js. It will not break existing applications and it does not look obfuscated to me. I am willing to submit a PR if such an API looks reasonable to you.

I’m running into this as well, building out static pages with the new 9.3 SSG functions. To me, ideally, this would behave exactly as @Janpot described in the third option in their above comment.

For our architecture, we would basically have a pages/[...path].jsx file that would catch both the index page (if defined as a route from getStaticPaths) as well as any other pages defined by our CMS.

@programbo the problem with that approach is because creates 2 lambdas one for […slug] and one for index.tsx

@timneutkens ok, is there a possibility to add this feature somehow? I’m working on a modern Rails equivalent for Next.js, and like a Rails controller, I want a single file for handling all my CRUD operations. I’d really like to avoid cluttering the file system with a bunch of re-export files. This type of REST API is so common, it seems like a great feature candidate.

Something else I intuitively tried was:

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

Personally I lean towards [] and ‘’.

Yeah agreed that’s probably the best way 👍

Concretely, making a page named: pages/hello/[[...world]].js would match /hello, /hello/1, and /hello/2/3, etc.

@Timer What should be the value of the world parameter when I access the route /hello? undefined or []?

I assume this syntax would also work for pages/hello/[[world]].js? What would be the value of world in this case? undefined or ''?

Personally I lean towards [] and ''.

Could you clarify what you mean by “optional routes”?

Optional route params, the thing that is asked for in the feature request.

@flybayer @yanv1991 if it’s helpful, as a workaround I’m using the experimental rewrittes in next.config.js

modules.export = {
  experimental: {
    rewrites: async () => [
      { source: '/', destination: '/index?slug=index&slug' }
    ]
  }
}

Of course, not quite the same. It’ll have /index as pathname instead of / and slug will be ["index"]. Also, using next/link is tricky since it isn’t aware of this rewrite 🤷‍♀

For now, it’s okay-ish (at least not two lambdas generated) but I’m also waiting to see any progress on this, so I can remove this rewrite and the ugly thing I have to make next/link work with this setup when it needs to go to the root path (with pre-fetching thing).

It originally tried to fix this issue, but turned into something completely different.

Also note that it links to a very specific comment, only talking about 404 vs 500 behavior.

This issue is about asking that index routes 200.

FYI this seems to have been fixed by https://github.com/vercel/next.js/pull/10502 and released 8 days ago in v9.4.2

https://github.com/vercel/next.js/releases/tag/v9.4.2

We should also throw a build error if you define both pages/hello/[[…world]].js and pages/hello.js or pages/hello/index.js.

👍

@Timer What’s the behavior of

/pages
  /hello
    /[[...world]].js
  /index.js

Would it error?

@laleksiunas I just started setting up some tests for it, but I’m not sure yet whether I’m going to finish this, so you can take it if you want.

It seems they use the ugly experimental re-routing hack themselves, lol.

We run experimental options against nextjs.org, vercel.com etc to test them against production workloads. I’d hardly call new features “ugly hacks”, it’s a new feature being adopted in projects we control.

~ can’t be used as a filename character btw, it won’t work on all platforms.

Something we’ve talked about it having [[optionalparams]] syntax for optional routes.

From the Dynamic Routing docs:

A good example of catch all routes is the Next.js docs, a single page called pages/docs/[…slug].js takes care of all the docs you’re currently looking at.

It seems they use the experimental re-routing feature themselves.

What about having another operator before the slug, something like pages/~[…slug].js

My 2 cents: something like ~path[...asQuery].js. Example:

pages/
  about/
    index.ts
  ~dashboard[...page].js
  ~blog[...post].js
  index.js

The following would match:

/about       -> pages/about/index
/dashboard*  -> pages/~dashboard with router.query.page populated
/blog*       -> pages/~blog with router.query.post populated
/            -> pages/index

This allows to easily see the difference between catch-all-paths routes and traditional folder-based routing, and allow customizing the name of the router.query variable. A default value for the queries could be index, or perhaps simply undefined.

What about this in the pages/index.js file?

export { default, getStaticProps, getStaticPaths } from './[...slug]'

I haven’t tested it yet, but it’s used in this example

@timneutkens is this feature going to be included soon? I have another problem having an index.js that exports the [...slug].js to catch the root index, it’s actually creating 2 separated lambdas.

And it seems like, even if I try to add a

// /api/users/index.js
export { default } from "./[...slug]";

It makes GET /pages/api/users work, but GET /pages/api/users/ gives me a 500. Without the index file that route 404s.