Infinite redirect loop in NextJS development environment
Description
I'm getting this error constantly after logging into an account. This is development environment and running on localhost
Package + Version
- [ ]
@clerk/clerk-js - [ ]
@clerk/clerk-react - [x]
@clerk/nextjs - [ ]
@clerk/remix - [ ]
@clerk/types - [ ]
@clerk/themes - [ ]
@clerk/localizations - [ ]
@clerk/clerk-expo - [ ]
@clerk/backend - [ ]
@clerk/clerk-sdk-node - [ ]
@clerk/shared - [ ]
@clerk/fastify - [ ]
@clerk/chrome-extension - [ ]
gatsby-plugin-clerk - [ ]
build/tooling/chore - [ ] other:
{
"dependencies": {
"@clerk/nextjs": "^4.21.12",
"next": "13.4.7",
}
}
I just ran into the same issue after updating my node to v.21. I resolved the issue by downgrading node to v.18 and updating nextjs to v.14.1.0. Hope this might help someone!
This will help a lot. Thanks!
But I want to know who else is still having this issue. I'm on node v.20.10.0 and I haven't noticed these issues for a long time, it comes back yes, but when I sync my time, I don't notice it again. The issue only comes back whenever I run into a low battery on my laptop.
We are also having this issue. Still have no clue how to fix it.
This is due to clerk not supporting multiple instances on the same host/domain name. I've asked clerk to call it out in the docs to avoid confusion. They have a future feature planned that would support instances across subdomain. Use another hostname and you won't hit the infinite redirect.
On Wed, Jan 31, 2024, 1:39 PM Rey @.***> wrote:
I just ran into the same issue after updating my node to v.21. I resolved the issue by downgrading node to v.18 and updating nextjs to v.14.1.0. Hope this might help someone!
This will help a lot. Thanks!
But I want to know who else is still having this issue. I'm on node v.20.10.0 and I haven't noticed these issues for a long time, it comes back yes, but when I sync my time, I don't notice it again. The issue only comes back whenever I run into a low battery on my laptop.
We are also having this issue. Still have no clue how to fix it.
— Reply to this email directly, view it on GitHub https://github.com/clerk/javascript/issues/1436#issuecomment-1919805667, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAG5UFXJ6CHKZPPCU2GUKDYRKMWDAVCNFSM6AAAAAAZYYYU6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJZHAYDKNRWG4 . You are receiving this because you were mentioned.Message ID: @.***>
@jameslporter yes I just figured out it must be something like that. I was running prod on mydomain.com and a staging instance on a subdomain, staging.mydomain.com. This caused the infinite loop. I found out that if I cleared my cookies etc. in Chrome and then viewed the subdomain, it would work (at least for a while), but once I viewed the prod instance things would be screwed up again.
Great to hear there is future work planned to support this across subdomains; would rather not have to pay for extra domains just to support a staging environment (unless that's a best practice for some reason etc.)
For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did
@dimkl Github repo :- https://github.com/Aayushpatil77/ecommerce-dashboard
Not found!
For me upgrading to
@clerk/nextjs=4.29.7,next=14.0.4did not help, but eventually clearing cookies and localstorage did
It happened only in development or even the production?
For me upgrading to
@clerk/nextjs=4.29.7,next=14.0.4did not help, but eventually clearing cookies and localstorage didIt happened only in development or even the production?
It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. https://github.com/vercel/next.js/issues/55648
Mainly
For me upgrading to
@clerk/nextjs=4.29.7,next=14.0.4did not help, but eventually clearing cookies and localstorage didIt happened only in development or even the production?
It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. vercel/next.js#55648
Mainly everyone should just upgrade to Next.js 14.1.3. It worked for me as I upgraded as well.
Mainly
For me upgrading to
@clerk/nextjs=4.29.7,next=14.0.4did not help, but eventually clearing cookies and localstorage didIt happened only in development or even the production?
It only helped for the initial page load but same issue after I refreshed. I have figured out the final fix now. Seem to be a Nextjs Issue. Upgrading to 14.1.3 solved the issue eventually. vercel/next.js#55648
Mainly everyone should just upgrade to Next.js
14.1.3. It worked for me as I upgraded as well.
Yes, upgrading my Next.js ('^14.2.1') worked for me. Just do npm install next@latest and it should install the latest Next.js which should resolve this issue with clerk.
We are experiencing the exact same error since releasing our production environment. The issue might be tied to the
__client_uatcookie which is available twice if both the develop and the main instance are running on the same domain.We have the following scenario:
- development on
a.b.c.mysite.io- main on
app.mysite.ioThis is the cookie we see on main:
__client_uat = <timestamp>This is what we see on develop:
- First cookie, domain "a.b.c.mysite.io":
__client_uat = <timestamp>- Second cookie, domain ".mysite.io":
__client_uat = 0Once one has visited the main site with their browser and switches to develop to test something, the site is trapped in an infinite loop with the error referring to the said cookie, ie.
The Cookie '__client_uat' has been rejected for invalid domain.We have to painfull clear the cookies, refresh everything and refrain from accessing the live site in the same session. It never occurs on
localhost. Anybody else having this issue?(Node v18.17.0, @clerk/nextjs v4.29.3, in Docker)
Same issue here. @stx-chris did you figure out a solution? Was it solved by updating to the latest packages?
Hi guys, I figured out that the issue is related to the local storage and session's keys. When I delete them it works as expected. I guess this happens when the authentication failed. This is my only for my case.
I got into redirect loop with these packages clearning localstorage and session keys does not help
"next": "^14.2.3",
"@clerk/nextjs": "4.30.1",
@hieutrluu Could you please open a new issue or reach out to [email protected] with a minimal reproduction? Thank you!
I found a rather dumb reason why I was being infinitly redirected in my end. In the middleware.ts setup, I had my RouteMatcher configured like so:
import { NextResponse } from "next/server";
const isProtectedRoute = createRouteMatcher(["/(.*)"]);
export default clerkMiddleware((auth, req) => {
if (isProtectedRoute(req)) {
auth().protect();
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!.+.[w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
The problem was that the createRouteMatcher method was configured to redirect every path starting with "/", which Im assuming included the /sign-up page and created the loop.
Changing the line to const isProtectedRoute = createRouteMatcher(["/"]); fixed my problem.
(I added the wildcard cause I wanted to make every route private, seems I might have to find another way...)
Everyone has different ways on how to solve this issue, mainly I'd just upgrade all my packages to latest, and make sure my date-and-time are correct
I faced the redirect issue due to middleware. Make sure, you include the sign in/sign up path in public path routes. Next, redirect won't happen anymore.
const isPublicRoute = createRouteMatcher(['/agency/sign-in(.*)', '/agency/sign-up(.*)', '/_next(.*)'])
Clerk needs your system clock to be in sync with your current timezone. I synced my system timezone correctly. The clerk is working fine.
On my Windows machine, I turned off set time zone automatically and set time automatically and it worked...!!!
So my users will need to sync their clock to the timezone or it's just happening for development?
Only in development. If your users have a misconfigured clock chances are that they are having problems accessing all types of web pages.
From: Greg Wozniak @.> Sent: Thursday, December 12, 2024 12:28:44 PM To: clerk/javascript @.> Cc: Vinicius Pinho @.>; Comment @.> Subject: Re: [clerk/javascript] Infinite redirect loop in NextJS development environment (Issue #1436)
So my users will need to sync their clock to the timezone or it's just happening for development?
— Reply to this email directly, view it on GitHubhttps://github.com/clerk/javascript/issues/1436#issuecomment-2539286976, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AXFVS2LBV7K5TSYE2QE34GL2FGTSZAVCNFSM6AAAAAAZYYYU6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMZZGI4DMOJXGY. You are receiving this because you commented.Message ID: @.***>
im getting the same error again with "next": "15.1.2" and "@clerk/nextjs": "^6.8.1"