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",
}
}
Nope I'm on 14 as well. It's not a code problem. It's how you are running it. Again the only way I've gotten it to work properly for http localdev is to use localhost:3000. Getting kind of frustrated with the lack of support here. I feel like I'm the clerk support staff trying to help others through the problems I experienced. I really just want confirmation that my hypothesis is correct and if so this be stated clearly in the documentation. My requests have fallen on deaf ears.
On Wed, Nov 15, 2023 at 9:30 AM stx-chris @.***> wrote:
thx @jameslporter https://github.com/jameslporter , seems to be related to changes in Next v14, see my comment in #2115 https://github.com/clerk/javascript/issues/2115. It might be a recently introduced bug in Next 14 or an issue with how Clerk determines the current protocol and hostname.
— Reply to this email directly, view it on GitHub https://github.com/clerk/javascript/issues/1436#issuecomment-1812752449, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAG5UDPU5UVDYYBFRBYDK3YETN27AVCNFSM6AAAAAAZYYYU6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJSG42TENBUHE . You are receiving this because you were mentioned.Message ID: @.***>
Ok in that case our issues are different, because local development and build work fine and we don't get any token-related issues. It's the same with Next v13 and v14.
But once we build our Docker image and upload it to Google Cloud, with the only difference being the Next.js version, Clerk's url debug value is http://localhost:3000 within the Docker container on v13 and https://localhost:3000 on v14 and the error reason we receive is cross-origin-referrer, which is expected in that case.
Unfortunately, there is no way we can fix how Next.js determines its nextUrl, but the SSL protocol is definitely wrong.
We have received one reproduction for Next 14 specific redirect issues. Engineering is looking st this. We encourage the creation of reproductions of your issue where and when possible, and posting those in https://github.com/clerk/javascript/issues/2115.
If only the people who knew the answer were here to help. Godspeed and good luck! Maybe if you share more about your docker setup we can find out what isn't quite right.
On Wed, Nov 15, 2023, 9:47 AM stx-chris @.***> wrote:
Ok in that case our issues are different, because local development and build work fine and we don't get any token-related issues. It's the same with Next v13 and v14.
But once we build our Docker image and upload it to Google Cloud, with the only difference being the Next.js version, Clerk's url debug value is http://localhost:3000 within the Docker container on v13 and https://localhost:3000 on v14 and the error reason we receive is cross-origin-referrer, which is expected in that case.
— Reply to this email directly, view it on GitHub https://github.com/clerk/javascript/issues/1436#issuecomment-1812782957, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAG5UEROSQUJLHYRVLKZ6TYETPYNAVCNFSM6AAAAAAZYYYU6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJSG44DEOJVG4 . You are receiving this because you were mentioned.Message ID: @.***>
@stx-chris @creative-tutorials @jameslporter After digging heavily into Next's internals, as well as performing a number of tests with SSL termination and reverse proxying, I'm fairly confident that the PR we submitted to Next.js earlier today, will solve these issues.
Currently, the PR is approved and, once merged, the changes will be available as a canary build of v14.0.3.
In the meantime, you should should be able to mitigate these issues by setting the X-Forwarded-Host request header to the Host header via Varnish, or otherwise.
This was my configuration via Caddy:
localhost {
reverse_proxy localhost:3000 {
header_up X-Forwarded-Host {host}
}
}
In short, if X-Forwarded-Host is already set, Next doesn't overwrite it with the incorrect value.
Awesome thanks for the update. I'm curious what kind of integration tests do you all have around clerk/nextjs and how do you go about testing new next releases to ensure regressions like this don't happen?
thx @jameslporter , seems to be related to changes in Next v14, see my comment in #2115. It might be a recently introduced bug in Next 14 or an issue with how Clerk determines the current protocol and hostname.
@stx-chris @creative-tutorials @jameslporter After digging heavily into Next's internals, as well as performing a number of tests with SSL termination and reverse proxying, I'm fairly confident that the PR we submitted to Next.js earlier today, will solve these issues.
Currently, the PR is approved and, once merged, the changes will be available as a canary build of
v14.0.3.In the meantime, you should should be able to mitigate these issues by setting the
X-Forwarded-Hostrequest header to theHostheader via Varnish, or otherwise.This was my configuration via Caddy:
localhost { reverse_proxy localhost:3000 { header_up X-Forwarded-Host {host} } }In short, if
X-Forwarded-Hostis already set, Next doesn't overwrite it with the incorrect value.
Thanks for the update
I was getting this error when using
nodev20, switching back to 18 resolved it for me
Thanks, It worked for me. i was on node v21, switched back to 18 and it's working
I still have this issue. I have cleared cache and cookies, I have gone from node v21 to v18, I have changed date and time on and off. I am using localhost:3000
I still have the issue as well on node 18. Works on localhost directly to the service. Doesn't work through my varnish front proxy that maps the hostnames to service backend host. The same varnish setup works in stage/prod as it's using https via AWS elb with certificate manager. I sent my har files to Colin.
I had same issue but I solved it.
The following is what we did this time.
- switch to node v18.18.2 (current latest version)
- install latest version of @clerk/nextjs
- update packages.
After changing node 20.10.0 to node 18.12.1, it working fine.
If anyone is still having this issue, please make sure your date-time and timezone are set to automatically update. clear cookies, and run your application in incognito mode. If this doesn't work try reconfiguring your application with Clerk again by following the docs.
cleared cookies, changed node version (v18.18.2), and she works :--) thanks all
For me the underlying problem is that clerk doesn't support multiple instances on the same domain. I had a subdomain per environment. They say they are working on removing this limitation but unclear when.
On Sun, Jan 7, 2024, 8:59 AM amateurbeekeeper @.***> wrote:
hmmm still broken on my end
— Reply to this email directly, view it on GitHub https://github.com/clerk/javascript/issues/1436#issuecomment-1880082783, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAG5UADTRDWPCOAFABDDXLYNKZ4PAVCNFSM6AAAAAAZYYYU6SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGA4DENZYGM . You are receiving this because you were mentioned.Message ID: @.***>
We are experiencing the exact same error since releasing our production environment. The issue might be tied to the __client_uat cookie 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.io
This 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 = 0
Once 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)
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!
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.
I'm running into this on node v21.6.1
I'm running into this on node v21.6.1
Try syncing your clock please