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 does not take rewrites into account

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
cvolantcommented, Apr 9, 2022

@srikanthkh I merely added a folder with the same name as the rewritten route and put my middleware in it…

0reactions
github-actions[bot]commented, Jul 18, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Middleware not getting executed when using rewrites #30635
I'm running into the same problem. It would be nice to import the rewrite file into the pages file to make it explicit...
Read more >
URL Rewriting Middleware in ASP.NET Core - Microsoft Learn
This article introduces URL rewriting with instructions on how to use URL Rewriting Middleware in ASP.NET Core apps. URL rewriting is the ...
Read more >
Next.js Middleware: How it Works and 5 Real Use Cases
Since middleware runs before sending the page to the user, it will not affect how pages are rendered, so it works with CSR,...
Read more >
next-http-proxy-middleware - npm
Try using Next.js Rewrites(recommended). This function is supported by Next.js. No additional libraries need to be installed! https:// ...
Read more >
Middleware - Django documentation
Middleware ¶. Middleware is a framework of hooks into Django's request/response processing. It's a light, low-level “plugin” system for globally altering ...
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