0
Challenge · 65Architecture & State

SWR

Ecoused in ~20% of apps

You already saw how raw useEffect fetching hides race conditions and re-fetches on every mount. SWR is Vercel's tiny answer: one hook, useSWR(key, fetcher), gives you cache, dedup, and background revalidation in a few hundred lines instead of pulling in a full query engine.

Core idea. useSWR(key, fetcher) returns cached data immediately (stale), kicks off a background fetch to confirm it's still current (revalidate), and swaps in fresh data when it arrives — keyed by a stable cache key that's shared across every component using it.
Playground, this isn't a screenshot
<ViewCount />no data yet
—

Press Visit to mount the component.

SWR CACHE (key: /views)
(empty)
SERVER (live, drifts)
1,280
visits: 0
// SWR activity log…

First visit blocks on the network. Every visit after serves the cached (stale) value instantly, then revalidates in the background and swaps in fresh data — that is the whole name made literal.

↑ play a little first, then dig in