Next.js Discord

Discord Forum

Bug occurs when I use `revalidate` and `router.push`

Answered
Scaly-naped Pigeon posted this in #help-forum
Open in Discord
Scaly-naped PigeonOP
'use server';

import { revalidateTag } from 'next/cache';

export default async function revalidate(tag: string) {
  revalidateTag(tag);
}



const onLoginSuccess = () => {
  // ...
  revalidate("accessToken");
  router.push("/");
}


URL is changed "/", but contents are not rendered
How to fix it? TT.
Answered by Ray
because you use router.push and there is a 30 sec router cache, you should use redirect in server action instead
View full answer

13 Replies

Scaly-naped PigeonOP
Oh!!..
fixed!
why I have to use refresh??
@Scaly-naped Pigeon why I have to use refresh??
because you use router.push and there is a 30 sec router cache, you should use redirect in server action instead
Answer
@Scaly-naped Pigeon why I have to use refresh??
or try this
export default async function revalidate(tag: string) {
  revalidateTag(tag);
  redirect("/")
}
and remove this from onLoginSuccess
-router.push("/");
-router.refresh();
Scaly-naped PigeonOP
It works very well.
I have one more question.
I use popup this service. When I use revalidate, Re-render occurs.
I don't know how to fix it.
or try to call the popup after the server action here
const onLoginSuccess = async () => {
  // ...
  await revalidate("accessToken");
  // show popup
}
Scaly-naped PigeonOP
It works!
Thank you so much TTT
@Scaly-naped Pigeon Thank you so much TTT
no prob