While I read SWR react hook documentation and Stale-While-Revalidate methodology, It seems that swr uses cached data just for a short time placeholder to show result to users quickly. (Don't get me wrong I still think swr has many benefits)
I'd like to compare SWR with HTTP static content cache to make things clear.
In terms of HTTP static content cache aka HTTP cache,
- Client fetches static content that comes with the
Cache-Control
orExpires
header. - Next time the same content needs to be fetched, As long as the cached file is available and valid based on
Cache-Control
orExpires
. It uses the cached data and doesn't send HTTP request to the server.
However, when it comes to swr useSWR,
- it saves HTTP response data to local cache.
- Next time the same data needs to be fetched. It uses data from cache(if exists) and sends HTTP requests to server(revalidate) to check if the data has changed.
I know HTTP cache and swr react hook has tons of more features but this is just a brief summary of HTTP cache and swr react hook.
My question is
if swr has to revalidates every time the cached data is used, it doesn't reduce the number of requests other than the concurrent requests of the same API URL from multiple components, right?
If then, is this cache mechanism for showing data quicker to users and may be preventing multiple concurrent requests of the same API URL from multiple components?
I am quite new to swr and please let me know if I misunderstood.
Thanks!
swr docs: https://swr.vercel.app/
HTTP cache on mozilla: https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching