SWR
The name “SWR” is derived from stale-while-revalidate, a HTTP cache invalidation strategy popularized by HTTP RFC 5861 (opens in a new tab). SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data.
"SWR"이라는 이름은 HTTP RFC 5861에 의해 알려진 HTTP 캐시 무효 전략인 stale-while-revalidate에서 유래되었습니다.
SWR은 먼저
- 캐시(stale)로부터 데이터를 반환한 후,
- fetch 요청(revalidate)을 하고,
- 최종적으로 최신화된 데이터를 가져오는 전략입니다.
Example
import useSWR from 'swr'
function Profile() {
const { data, error, isLoading } = useSWR('/api/user', fetcher)
if (error) return <div>failed to load</div>
if (isLoading) return <div>loading...</div>
return <div>hello {data.name}!</div>
}
Usage with Next.js
See also
- react-query (TanStack Query)
- SWR (stale-while-revalidate)