v12.2.x
Middleware is invoked multiple times for a page for the same route request
#38515
Closed
1 task done
Labels
Verify canary release
Provide environment information
What browser are you using? (if relevant)
Chrome & Firefox
Describe the Bug
When using Middleware for a page, it is invoked multiple times for the same route request.
Here's a very basic example, an app with another
/test
route. In the Middleware, we log whenever it is invoked by a request for this page:Going to this page in the browser, the result is two
console.log
s, even though it should be one:There is a similar recent issue, but there, the problem were
HEAD
requests. Looking at my example, the double invocation is done with twoGET
requests, so I think this issue is not related (or not fixed with whatever was the problem there).Expected Behavior
Only one invokation per page request.
Link to reproduction
https://github.com/bennettdams/nextjs-middleware-invocation-bug
To Reproduce
Here is a reproduction repo you can use.
Or, here are the steps to manually reproduce the issue:
Install via
npx create-next-app@latest --ts --use-npm .
Add a test page
Add Middleware that logs whenever it is invoked for our new
Test
pagenpm run build
andnpm run start
Go to
/test
in the browser. Reload the page.I experienced this behavior for
v12.2.0
,v12.2.1
andv12.2.2
.As a comparison, I also checked this for
v12.1.6
. There, this problem does not exist. There's only one invocation as expected. To reproduce in the same repo:npm i next@12.1.6
middleware.ts
to_middleware.ts
and move it to the "pages" folder.The text was updated successfully, but these errors were encountered: