Middleware does not take rewrites into account
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #40-Ubuntu SMP Mon Mar 7 08:03:10 UTC 2022
Binaries:
Node: 14.17.0
npm: 6.14.13
Yarn: 1.22.17
pnpm: N/A
Relevant packages:
next: 12.1.1-canary.17
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)
next start
Describe the Bug
When using rewrites, the middleware dedicated to the destination page is not executed: the middleware dedicated to the source page is executed instead, if it exists.
/pages
├ ...
├ /news
| ├ _middelware
| └ [...slug]
├ /blog
| └ _middelware
├ ...
And in next.config.js:
// ...
async rewrites() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*',
},
]
},
// ...
In this case, visiting /blog/something
display the /news/[...slug]
page with the { slug: 'something' }
query, but the associated /news/_middleware
is not executed. Instead the /blog/_middleware
(associated with no pages: /pages/blog
does not contain any) is executed.
Expected Behavior
I expect the _middleware associated with the rewrite destination page to be executed and not the one associated with a hypothetical source page.
To Reproduce
Here is a minimal reproduction: https://github.com/cvolant/next-middleware-rewrite-issue
Reproduction steps are described in the readme file.
Issue Analytics
- State:
- Created a year ago
- Reactions:11
- Comments:6 (2 by maintainers)
Top GitHub Comments
@srikanthkh I merely added a folder with the same name as the rewritten route and put my middleware in it…
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.