Inconsistency dev/prod: Middleware Execution
Verify canary release
- [X] I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: #1 SMP Mon Sep 19 19:14:52 UTC 2022
Binaries:
Node: 16.16.0
npm: 8.19.1
Yarn: 1.22.19
pnpm: 7.11.0
Relevant packages:
next: 12.3.2-canary.43
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next dev, next build && next start
Describe the Bug
Depending on whether next dev or next build && next start is used, the middleware's behaviour changes.
When started with next dev, every route change executes the middleware but when started with next build && next start, the middleware is executed only once. On a second rerender of the page does not trigger the middleware.
(ex. With next start: A page that was previously rewritten on a condition with NextResponse.rewrite() stays rewritten until a page reload or new browser request occurs. With next dev: The page updates as expected)
Consequently there are things working in dev which wont work in production.
Quite similar to Issue #40305, only that there next dev and next start seem to behave the same way.
Issue #39715 and #39641 might have the same underlying issue.
Expected Behavior
I am not sure which behavior is intended but I expected it to be consistent between next dev and next start.
The more intuitive behavior is the dev one for sure 🤗
Link to reproduction
https://github.com/theswampire/nextjs-middleware-inconsistency
To Reproduce
Part 1: dev Behaviour
- Start Application using
next dev - Navigate around while observing the server logs
- Middleware is called on every page change
Part 2 prod Behaviour
- Delete
.nextfolder and start the application usingnext build && next start - Play around and observe server logs
- Middleware is called only on the first page render. A SSR page always triggers the middleware