using onErrorRetry without compromising on Exponential backoff algorithm #1574
Replies: 3 comments 3 replies
-
I think this behavior would be a great addition to the library. Would the team accept a contribution to add a configuration which allows bailing on certain errors without overriding the entire error retry process? I think it would make sense to be able to provide a function that receives the error and returns a boolean on whether to proceed with the default retry logic. I could see this being a new config or updating Line 307 in de17eb6 could be updated to something like if ((typeof config.shouldRetryOnError === 'function' && config.shouldRetryOnError(err)) || (typeof config.shouldRetryOnError === 'boolean' && config.shouldRetryOnError)) { then if you want to keep using exponential backoff but bail only on 401s for example, you could provide the following shouldRetryOnError: (err) => err.statusCode !== 401 |
Beta Was this translation helpful? Give feedback.
-
Was looking for the same thing and stumbled on this discussion. But it seems this feature was actually added in 1.2.1: https://github.com/vercel/swr/releases/tag/1.2.1 (it's not just not well documented right now) So, thanks! |
Beta Was this translation helpful? Give feedback.
-
For anyone who finds this thread in the future, you can simply delegate to the original SWR implementation of import { defaultConfig } from "swr/_internal";
<SwrConfig value={{
onErrorRetry(error, ...args) {
if ( ... ) {
// Your custom logic
} else {
// Otherwise use the default behavior
defaultConfig.onErrorRetry(error, ...args);
}
}}
/> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Essentially I want to do these
404
I am using
onErrorRetry
in globalSWRConfig
for this purpose.but it seems that I do not retry exponentially after I provide
onErrorRetry
, and it is well documented tooIs there any way, that I can stop retrying for
404
errors without compromising on the Exponential backoff algorithm for other types of errorsClick here for the codesandbox with demonstration
Beta Was this translation helpful? Give feedback.
All reactions