javascript icon indicating copy to clipboard operation
javascript copied to clipboard

Infinite redirect loop in NextJS development environment

Open ghost opened this issue 3 years ago • 101 comments

Description

I'm getting this error constantly after logging into an account. This is development environment and running on localhost

screenshot

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",
  }
}

ghost avatar Jun 29 '23 16:06 ghost

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.

flean avatar Jan 31 '24 19:01 flean

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 avatar Jan 31 '24 19:01 jameslporter

@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.)

eraoul avatar Jan 31 '24 22:01 eraoul

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

dnyn92 avatar Feb 14 '24 21:02 dnyn92

@dimkl Github repo :- https://github.com/Aayushpatil77/ecommerce-dashboard

Not found!

alamenai avatar Mar 09 '24 21:03 alamenai

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It happened only in development or even the production?

alamenai avatar Mar 09 '24 21:03 alamenai

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It 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

dnyn92 avatar Mar 14 '24 18:03 dnyn92

Mainly

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It 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.

creative-tutorials avatar Mar 22 '24 15:03 creative-tutorials

Mainly

For me upgrading to @clerk/nextjs=4.29.7,next=14.0.4 did not help, but eventually clearing cookies and localstorage did

It 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.

yuvrajzingh avatar Apr 14 '24 11:04 yuvrajzingh

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)

Same issue here. @stx-chris did you figure out a solution? Was it solved by updating to the latest packages?

nikos-pblworks avatar Apr 23 '24 13:04 nikos-pblworks

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.

alamenai avatar Apr 30 '24 22:04 alamenai

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 avatar May 07 '24 05:05 hieutrluu

@hieutrluu Could you please open a new issue or reach out to [email protected] with a minimal reproduction? Thank you!

jescalan avatar May 07 '24 17:05 jescalan

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...)

Vipigal avatar May 13 '24 01:05 Vipigal

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

creative-tutorials avatar May 13 '24 06:05 creative-tutorials

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(.*)'])

i-am-chitti avatar Aug 18 '24 17:08 i-am-chitti

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...!!!

devtm1123 avatar Oct 01 '24 13:10 devtm1123

So my users will need to sync their clock to the timezone or it's just happening for development?

g-wozniak avatar Dec 12 '24 15:12 g-wozniak

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: @.***>

Vipigal avatar Dec 13 '24 01:12 Vipigal

im getting the same error again with "next": "15.1.2" and "@clerk/nextjs": "^6.8.1"

zohairhadi avatar Feb 06 '25 06:02 zohairhadi