The neighbourhood head answers the villagers from his box of copies or sends Dodi running to the sekretariat: network-first, cache-first or…
Why it matters
The network is slow and sometimes down; a cache is quick but can go stale. The wrong strategy makes an app slow, shows old data, sends a POST again, or shows another account’s data. Service workers, SWR and React Query use exactly the same terms.
The key idea
Quick and fresh pull against each other
Common mistakes
Cache-first data stale forever While a copy is there, cache-first never asks the server again. For data that changes, use stale-while-revalidate or network-first, or give the copies an age and delete the expired ones.
SWR sends a POST again SWR calls its fetcher again whenever the page is opened again, the window is focused again, or the network comes back. That is safe for a GET, not for a POST, PUT or DELETE. Send a change once from its handler and let SWR only read. The Cache API itself refuses to keep a POST.
The old account’s data shows after a switch The old account’s copies are still in the cache. Delete the cache on logout (caches.delete(name)), or put the account’s id in the cache key.
Try it in this playground
Ask the schedule twice with cache-first The first takes 800 ms, the second a few ms: Dodi does not run again.
Open The schedule changed and ask Cache-first answers 15.00 though the sekretariat is on 16.30.
Close the road, ask through network-first Once fetch fails, the copy from the box answers.
Change the schedule, then ask twice through SWR The first is still old, the second is new, and both are quick.