Next.js Discord

Discord Forum

`revalidateTag` does not seem to update the clientside

Unanswered
Ragdoll posted this in #help-forum
Open in Discord
RagdollOP
So, I have a function for adding packages to a cart, which should refetch the cart after being executed.
But it doesnt seem to work at the moment.

The cart is fetched via a server action called getBasket, which uses a fetch with a nextjs tag.
(the tags in revalidateTag and the fetch ofcourse match)

Code example:

"use client";

export default function Page() {
  const [basket, setBasket] = useState<Basket | undefined>(undefined);

  useEffect(() => {
    getBasket().then((resp) => setBasket(resp));
  }, []);

  return <>{JSON.stringify(basket)}</>
}


I would assume that I am perhaps fetching the data in a way which isnt supported by tag revalidation?
And if so, how would I fetch it correctly?

8 Replies

RagdollOP
Do you have any tips/ideas as to how this might be resolved?
Aka, how I would force a resync of the basket for the client.
@Ragdoll Do you have any tips/ideas as to how this might be resolved? Aka, how I would force a resync of the basket for the client.
or try redirect(path, RedirectType.replace) after the revalidateTag
RagdollOP
Have you used SWR? If so, do you think it would be useful in this situation?
I've been reading through the nextjs docs, and it seems like it would be a fairly good solution
As it seems like you should be able to revalidate specific client queries/fetches, unless I've misunderstood it
@Ragdoll Have you used SWR? If so, do you think it would be useful in this situation?
yes you can fetch the initial data on server and pass it to the client and attach the initialdata to swr hook