Redirect in client-component doesn't work.
Unanswered
Barbary Lion posted this in #help-forum
Barbary LionOP
Hello, someone knows why it's not redirecting me? The redirectURl is defined, I logged it. It's "/dashboard" in a client component:
This part is triggered when the submit button in my form is clicked. Event: onSubmit
const redirectURL = searchParams && searchParams.redirectURL;
if (redirectURL) router.push(redirectURL);This part is triggered when the submit button in my form is clicked. Event: onSubmit
30 Replies
Munchkin
Have you set:
?
also which router are you using??
if you are using the app structure you need to use:
const router= useRouter();?
also which router are you using??
if you are using the app structure you need to use:
import { useRouter } from "next/navigation";not the other one. forgot what it was again
@Munchkin Have you set:
jsx
const router= useRouter();
?
also which router are you using??
if you are using the app structure you need to use:
jsx
import { useRouter } from "next/navigation";
Barbary LionOP
Yeah, sure. I imported from next/navigation
Munchkin
hmm just checking could you put
router.push(redirectURL) in {}@Munchkin hmm just checking could you put `router.push(redirectURL)` in `{}`
Barbary LionOP
Sure, but why? That doesn't affect it at all
Here, made it likes this
Output
Munchkin
I noticed it gave me different output sometime. Kind of an hail mary.
May i ask what you document structure looks like? are you using app or router?
May i ask what you document structure looks like? are you using app or router?
Barbary LionOP
Okay, I'm using app.
sry
Munchkin
all good. just checking.
just to check but could you try replacing router.push with
window.location.href = redirectURL?Barbary LionOP
Okay, I just found out that it's something with my router.
Munchkin
also just to make sure
router.push(${redirectURL})ah ok
Barbary LionOP
For some reason, the cookie is not set
But it is, the redirect happens after the request to the API is completed. Inside the API I set the cookie
Siberian Rubythroat
Ask ChatGPT-4
Also try with new URL
Also provide more context
What in redirectURL prop you passed to
Also try with new URL
Also provide more context
What in redirectURL prop you passed to
router.push() ?@Munchkin ah ok
Barbary LionOP
So I've found out, that something is wrong with the caching as it finds the cookie after page reload.
Siberian Rubythroat
Ok
Mark this as solved and close post
Barbary LionOP
Not solved mate
Munchkin
also its not a smart idea to ask everything from chatgpt. that way ur not learning anything.
Anywho. What you could do is useEffect to wait untill it gets the cookie and THEN push
Anywho. What you could do is useEffect to wait untill it gets the cookie and THEN push
Barbary LionOP
That's actually not the problem. It's some caching policy
Munchkin
so if i understand correctly you have to wait for the cache before it send you the cookie??
@Munchkin so if i understand correctly you have to wait for the cache before it send you the cookie??
Barbary LionOP
No, it has only the cookie after I refresh the page. That's due some caching.
I just need to find out how to bypass that
Munchkin
ðŸ‘
Barbary LionOP
Seems to be known https://github.com/vercel/next.js/issues/49865