`revalidateTag` does not seem to update the clientside
Unanswered
Ragdoll posted this in #help-forum
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
(the tags in revalidateTag and the fetch ofcourse match)
Code example:
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?
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
@Ragdoll 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:
ts
"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?
it is because revalidateTag only update the cache on the server
revalidateTag only invalidates the cache when the path is next visited. This means calling revalidateTag with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.
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.
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.
I think you can try
router.refresh()@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 revalidateTagRagdollOP
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