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 support middleware

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

16.10.0

What browser are you using?

Doesn’t matter

What operating system are you using?

macOS

How are you deploying your application?

next start, next dev

Describe the Bug

My application has a catch-all route.

/pages
└─catch_all
   └─[...slug]
      └─index.js

It works as expected: all requests to /catch_all/**/* is handled by this route.

But when I try to add _middleware to directory with catch-all route like this:

/pages
└─catch_all
   └─[...slug]
      ├─_middleware.js
      └─index.js

In dev mode application freezes with this error, when route is requested:

wait  - compiling /catch_all/[...slug]/_middleware...
error - HookWebpackError: Catch-all must be the last part of the URL.
error - unhandledRejection: HookWebpackError: Catch-all must be the last part of the URL.

On next build application build crashes with this error:

> Build error occurred
Error: Catch-all must be the last part of the URL.
    at UrlNode._insert (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:56:19)
    at UrlNode._insert (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:138:40)
    at UrlNode._insert (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:138:40)
    at UrlNode.insert (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:8:14)
    at /Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:161:46
    at Array.forEach (<anonymous>)
    at Object.getSortedRoutes (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/shared/lib/router/utils/sorted-routes.js:161:21)
    at /Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/build/index.js:247:44
    at Span.traceFn (/Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/trace/trace.js:67:20)
    at /Users/aozhigin/Documents/education/catch-all-routes-app/node_modules/next/dist/build/index.js:239:85

Expected Behavior

Added _middleware runs for nested catch-all routes.

For example, in submitted repo, when requesting route /catch_all/s/l/u/g/s/l/u/g/slug/, I expect to see this message in console:

In middleware for catch_all subroute slug. Pathname /catch_all/s/l/u/g/s/l/u/g/slug/

I don’t see this bug when adding middleware for dynamic route with single slug (e.g. /pages/dynamic_route/[slug]/_middleware.js), so it’s only logical that this should work for catch-all routes.

To Reproduce

You can reproduce this bug in this repo https://github.com/showmeyourhits/nextjs-catch-all-middleware.

  • Clone the repo
git clone git@github.com:showmeyourhits/nextjs-catch-all-middleware.git
  • Install deps
npm install
  • switch to branch with added middleware
git checkout added_catch_all_middleware

Dev mode error

  • start app in dev mode
npm run dev
  • go to
http://localhost:3000
  • click on Go to catch-all subroute. Application freezes. Other navigation attempts (click on other links/page refresh) will produce the error in console.

Build error

  • build the app
npm run build

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
balazsorban44commented, Feb 16, 2022

This will likely be fixed by https://github.com/vercel/next.js/pull/32601

1reaction
Schnizcommented, Jun 16, 2022

I believe this is not the case anymore now that we have a single root middleware 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Catch all route EXCEPT for /login - Stack Overflow
I'm not sure what you want to happen when a user accesses /login or / , but you can create ... Another way...
Read more >
NextJS with a root level catchall route for all URLs dynamic ...
For a first attempt at a middleware it looks decent. It is a bit on ... Those functions likely do not need to...
Read more >
catch-all route handler - Google Groups
I'm trying to override the default 404 behavior in express and I can't quite figure out how to do it. when my app...
Read more >
Dynamic Routes - Next.js
Dynamic Routes are pages that allow you to add custom params to your URLs. ... Defining routes by using predefined paths is not...
Read more >
A Guide to Error Handling in Express.js | Scout APM Blog
Error handling often doesn't get the attention it deserves. ... the first middleware is fired, followed by the corresponding route handler ...
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