[DO NOT MERGE] Reproduce infinite stacking cookies #2377
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR demonstrates how to reproduce #1917.
To run this, it is important to run in production mode:
cd examples/with-shadcnpnpm installpnpm run buildpnpm run startAdditionally, ensure to configure the
.envfile.What is this reproduction doing?
This is a very basic setup, nothing weird going on here at first sight.
However ... As we are using a Link component on the homepage for these 3 routes, the following happens:
Linkin the viewport.Now refresh the page, and u have 3 more. So 6.
Now refresh the page, and u have 3 more. So 9.
...
So any page-load when not being logged in, or when the middleware tries to initiate a redirect to auth/login in your own code, will end up with
ncookies, wherenis the amount ifLinkcomponents in the viewport that refer to a protected route.Once you click any of the actual buttons, it will call the middleware again (as it returned a non-200 code, it will hit the middleware again), set a cookie and go to auth0 and come back. Never using, or deleting the cookies that come from the page-load.
It seems to be very unreliable in next.js to skip the middleware on prefetch here. Currently, the solution seems to be to either:
prefetchto false on theLink. This way, prefetch does not happen anymore on page-load in production mode. On click, we still have an additional cookie, but it should not stack infinitely anymore.<a>instead of<Link>enableParallelTransactionsto false, which has the consequence that logging in in different tabs simultaneously will result inInvalid State./auth/callbackAdditionally, I find many references to detecting prefetching in the middleware is very unreliable, and I have not been able to get that working at all.
Even though some have a solution, it does not seem to work for me when using Next.js 15.