-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v12.2 up to v12.3.2-canary.22 handleHardNavigation Invariant: attempted to hard navigate to the same URL #38171
Comments
Minimal code example to demo the bug. GitHub: https://github.com/FDMediagroep/router-bug Demo app deployed on Vercel: Fixed since Navigate to https://router-3zllgxvkw-fdmediagroep.vercel.app/nieuws to see the error in the browser debug console. |
Did you have any luck fixing this issue? Currently facing the same thing when handling locales in the middleware. If I remove the middleware file, the 404 returns error free. |
I've even tried with middleware config to exclude all URLs starting with
I see that the config has effect but the error remains. |
So far this has worked for me:
|
In my case I am experiencing this error when I am on 404 page (non existing route) and on that page i try to navigate to the same non existing route using next link. The issue persists on both next info:
|
@balazsorban44 Please help check this problem. |
I am facing the same issue, only deployed on vercel.
I have a middleware up and running, but I am not sure if this is the initiator. import { NextRequest, NextResponse } from 'next/server'
const PUBLIC_FILE = /\.(.*)$/
export default function middleware(request: NextRequest) {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes('/api/') &&
!request.nextUrl.pathname.includes('/_next/') &&
request.nextUrl.locale === 'default'
if (shouldHandleLocale) {
const url = request.nextUrl.clone()
url.pathname = `/de-AT${request.nextUrl.pathname}`
return NextResponse.redirect(url)
}
return NextResponse.next()
} The compiled output shows the file in place: |
Currently experiencing this issue as well when not using any _middleware. |
I am experiencing this issue with empty middleware too, on Pages on dynamic routes are hard loaded, non-dynamic ones are fine. After navigating to a dynamic path, then back to a non-dynamic path, SWR content is hard fetched, not using the cache. My export default async function middleware() {} There are two error messages in the browser's console:
and:
Local builds work fine. |
This issue is absolutely destroying my error reporting. Has anyone been able to find a way to suppress the warnings until we find a bugfix? |
I haven't seen this error message with v12.2.3 anymore. So maybe you can try that version. |
I am still seeing this issue on v12.2.3
You can see the issue in action here Node: 16.x I have a single middleware that looks like this // <project root>/middleware.js
import { NextResponse } from "next/server";
export function middleware(request) {
// Added from suggestion above
// UPDATE: I removed this if and it also is still broken
if (request.nextUrl.pathname.startsWith("/_next")) {
return NextResponse.next();
}
if (request.nextUrl.pathname.startsWith("/tags")) {
return NextResponse.redirect(new URL("/news", request.url));
}
if (request.nextUrl.pathname.startsWith("/category")) {
return NextResponse.redirect(new URL("/news", request.url));
}
if (request.nextUrl.pathname.startsWith("/author/")) {
return NextResponse.redirect(new URL("/about", request.url));
}
} |
Cannot confirm @willemliufdmg. I've updated my packages and deployed it: The message is still there.
|
I've updated the original post to reflect the most current environment information. In the |
In my case, I also use dynamic route and just adding an empty middleware causes infinite reload with the above error. However if i change in korean, it works with the error but it's a bit slow. If I remove the empty middleware file, it's fast and everything works fine.
|
My issue has been resolved since updating to 12.2.4 |
Same: My issue has also been resolved since updating to 12.2.4. |
Hi, taking another look at the initial reproduction it looks like this isn't working correctly as the rewrite is dependent on the search value which is causing a rewrite to occur for a direct visit but not on a client transition since the The rewrites done in middleware need to match between direct visit and client transition otherwise the query hydration can fail to resolve correctly. After fixing the rewrite logic in the reproduction it appears to be working correctly in If you're still having troubles with this after fixing the above code issue in the middleware please provide additional information and we can investigate further! |
I was experiencing this error as well, so for those struggling with this a well. In my case I had a fallback rewrite (for incremental adaption) for all paths in the next.config.js, I would expect this rewrite not to be triggered after rewriting to a next route from the middleware. After removing this fallback in the config, my rewrites from the middleware work as expected. I was able to debug the function that created this error, and it said it would rewrite externally (externalDest: true). To fix it I'll be moving all rewrites from config to the middleware. Hopefully that solves the issue for good. |
We're having this same error, starting when we updated to v12.2.5 and persisting in v12.3.0. We have all our rewrites all in middleware and there aren't any fallbacks in config. We haven't been able to replicate the problem ourselves (locally or on Vercel), but our error logging is being flooded. The problem only goes away if we revert to v12.1.6. |
I've updated the demo to use the latest canary |
For anyone coming back to this who happens to be combining multiple different nextjs projects... Make sure your projects all have a basePath parameter set in their individual next.config.js files The rule is you can do multiple repos and rewrites, but they can't have the same paths, including / so, TL;DR if you have two projects, #1 is "home" and #2 is "blog" Make sure the blog next.config.js has this:
and then in your rewrites, change to
Not sure that this is exactly the issue others are having, but got here from Googling the same error message and this was my issue |
in my case i am deploying in azure with docker and i have the same error |
My team and I are experiencing this error yet. We are using the Following the documentation I could notice that we have some redirects on our So we believe that it could be something in middleware. Our middleware is in the root as the next upgrade guide to Besides that, I noticed in Sentry that the mostly errors is like It's really frustrating to not be able to reproduce it to have sure of how we can handle and try to solve this error. So any help and suggestion that could help us are welcomed. |
You can reproduce the problem with the Demo I linked to in the original post. The Demo has been updated to use |
This issue has been closed but the problem has not been resolved. |
@willemliufdmg as mentioned in #38171 (comment) the bug is in the middleware handling itself and not Next.js as it's handling the These requests must match or it causes a divergence on the client. Potentially we could add an error for this although it's tricky to detect. |
@ijjk It would be great if Vercel could provide more official documentation, recommendations, or mitigation for this issue. I've put a lot of time into trying to resolve this error with no success. I don't believe my middleware is doing anything that's not demonstrated in official examples, but the error persists. I feel blocked from updating nextjs because of this. |
I see this error pop up in my Sentry and I have zero middlewares. |
This error also shows during local development. I can only assume that it has nothing to do with Vercel at that time but I could be wrong. |
In my project (no middleware and rewrites), this error happened when user navigate to same current URL (e.g: in homepage, user click logo header) after a new deployment |
Hi, we have recently updated how this specific case is handled and the initial reproduction should no longer be causing issues in |
I can confirm that this case has now been fixed in next@12.3.2-canary.25. Thank you all@next.js for solving this bug! |
just DOUBLE check the href at Link |
Same issue on Next 13.0
|
Just updated to Next 13.0 and i am still facing the same issue. Sentry is getting destroyed by this error. |
Updated to 12.3.2 and still have this issue. |
@shadowgroundz @dimitrisdi @maria404 I don't think the Next team will act on a closed issue, so might be better to open a new one. |
Hi, if you are still seeing this error it appears to be unrelated to the initial issue described here as that is now resolved, please open a fresh issue with reproduction. |
Must I open a new ticket for same issue while that still exist on newer version? |
@shadowgroundz What they mean is, if you're still getting the error, then it must have a different root cause than the issue in this ticket. For that, you should open a new ticket so the team can examine your issue. |
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. |
Verify canary release
Provide environment information
Deployed to Vercel
What browser are you using? (if relevant)
Not relevant
How are you deploying your application? (if relevant)
Vercel
Describe the Bug
It seems that since Next.js v12.2 the internal router does an extra fetch for the current page.js. This didn't happen in the older versions of Nextjs (see screenshots below). If the current URL is a dynamic route and is rewritten (our case in Middleware) the extra page.js fetch will result in a 404. This causes an attempt to reload the current page which then runs into the
handleHardNavigation
errorInvariant: attempted to hard navigate to the same URL
.Current production Next.js v12.1:
Develop Next.js v12.2:
Expected Behavior
Should just load the page without errors.
Link to reproduction
https://router-3zllgxvkw-fdmediagroep.vercel.app/nieuws
To Reproduce
Minimal reproduction repo: https://github.com/FDMediagroep/router-bug
Next Middleware rewrite rules:
Navigating to
/nieuws
will result in the error as described in the bug description for Next.js since v12.2. But not on the older version v12.1.The text was updated successfully, but these errors were encountered: