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.

Middleware in a [dynamic] route subdirectory runs against parent urls

See original GitHub issue

Verify 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
balazsorban44commented, Apr 7, 2022

The fix has been reverted as it seemed to break in certain cases. I’m reopening because this needs further investigation.

1reaction
balazsorban44commented, Mar 29, 2022

This has recently been fixed in #32601. Please give canary a try!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS Middleware Matcher For Dynamic Routes
I'm building a website with subdomains.
Read more >
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 >

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