Revalidate -- how to avoid browser cache? #558
-
Let's say we have an Potential ways out of this situation:
As far as I can see, So I thought it might be appropriate to target the fetcher directly, causing the fetcher to revalidate with different cache pragma: const invalidations = new Map<string, boolean>();
export const invalidate = (key: string) => {
invalidations.set(key, true);
};
export const jsonFetcher = async (key: string) => {
const shouldReload = (key: string) => invalidations.get(key) || false;
const requestInit: RequestInit = {
cache: shouldReload(key) ? "reload" : "default",
};
const res = await fetch(key, requestInit);
invalidations.delete(key);
return res.json();
}; Used like so: const me = useSWR<{ name: string}>("/api/me", jsonFetcher);
const newName = "Steve";
me.mutate(async (prev) => {
await updateNameOnBackend(newName);
invalidate("/api/me");
return {
...prev,
name: newName;
}
}, true); // << revalidate = true causing revalidation This works great and let's However, a developer experience issue with this becomes more apparent when I hide away the const me = useMe();
const newName = "Steve";
me.mutate(async (prev) => {
await updateNameOnBackend(newName);
invalidate("/api/me");
...
}, true); My code needs to know about the Change/feature proposal:
All other ideas welcome |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
If That means that updating a REST resource followed by a GET on that rest resource, should give you a fresh copy. |
Beta Was this translation helpful? Give feedback.
-
I can't be the only one to be using browser caching in conjunction with Working on high-traffic sites, doubling, tripling the number of requests that get made for a resource isn't going to fly because the order of magnitude is ever higher, therefore browser caching and edge caching are a must. That's all well and good, but sometimes you need to overcome that browser caching, and Does no-one else have this problem? I'm surprised! |
Beta Was this translation helpful? Give feedback.
-
I think I've realised the answer: don't apply
|
Beta Was this translation helpful? Give feedback.
I think I've realised the answer: don't apply
cache-control
, even a shortprivate
one, to things thatmutate
. This avoids the issue altogether./post/1234
withcache-control: max-age=60
then updating/post/1234
still has the same sort of problem, but maybe it's not such a big issue. IDK, I'm happy to assume it's not an issue that affects most people.