shouldRetryOnError accepts a functionPreviously shouldRetryOnError is either true or false. Now it accepts a function that conditionally determines if SWR should retry. Here's a simple example:
const fetcher = url => fetch(url).then(res => {
// Fetcher throws if the response code is not 2xx.
if (!res.ok) throw res
return res.json()
})
useSWR(key, fetcher, {
shouldRetryOnError: (error) => {
// We skip retrying if the API is returning 404:
if (error.status === 404) return false
return true
}
})
Thanks to @sairajchouhan for contributing!
shouldRetryOnError accepts a function that can be used to conditionally stop retrying by @sairajchouhan in https://github.com/vercel/swr/pull/1816Full Changelog: https://github.com/vercel/swr/compare/1.2.0...1.2.1
Fetched April 18, 2026