Middleware in a [dynamic] route subdirectory runs against parent urls
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000
Binaries:
Node: 16.14.2
npm: 8.5.0
Yarn: 1.22.18
pnpm: N/A
Relevant packages:
next: 12.1.0
react: 17.0.2
react-dom: 17.0.2
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
With a page structure like:
// pages/index.js
// pages/[customer]/_middleware.js
// pages/[customer]/dashboard.js
And a pages/[customer]/_middleware
file containing:
export function middleware(req: NextRequest) {
console.log(req.url)
}
I would expect _middleware
to only apply to pages that resolve within that subdirectory, however it gets applied to every request:
wait - compiling /[team]/_middleware...
event - compiled client and server successfully in 114 ms (765 modules)
http://localhost:3000/api/auth/session
http://localhost:3000/api/session
wait - compiling /api/auth/[...nextauth]...
wait - compiling /api/session...
event - compiled client and server successfully in 86 ms (779 modules)
http://localhost:3000/customer/1/dashboard
http://localhost:3000/_next/static/chunks/main.js?ts=1648344461416
http://localhost:3000/_next/static/chunks/webpack.js?ts=1648344461416
http://localhost:3000/_next/static/chunks/react-refresh.js?ts=1648344461416
http://localhost:3000/_next/static/chunks/pages/_app.js?ts=1648344461416
http://localhost:3000/_next/static/development/_buildManifest.js?ts=1648344461416
http://localhost:3000/_next/static/development/_ssgManifest.js?ts=1648344461416
http://localhost:3000/_next/static/chunks/pages/_error.js?ts=1648344461416
http://localhost:3000/_next/static/development/_middlewareManifest.js?ts=1648344461416
http://localhost:3000/api/session
http://localhost:3000/api/auth/session
http://localhost:3000/favicon.ico
Expected Behavior
I would expect the middleware not to be invoked on an unmatched route.
To Reproduce
See above description with a trivial example.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
A guide to using React Router v6 in React apps
This can be handled by using a routing library such as React Router. In this tutorial, let's take a look at how to...
Read more >ASP.NET Core Localization Deep Dive - Joonas W's blog
Here we use a router to grab the culture from the URL and set up the route data before hitting the localization middleware....
Read more >Bigger Applications - Multiple Files - FastAPI
There's a subdirectory app/routers/ with another file __init__.py , so it's a "Python ... then go to the parent of that package (there's...
Read more >2 Oracle WebLogic Server
Application Testing Links Fail to Resolve in Administration Console ... and the server is running on a dual stack (IPv6/IPv4) machine where Java...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The fix has been reverted as it seemed to break in certain cases. I’m reopening because this needs further investigation.
This has recently been fixed in #32601. Please give
canary
a try!