Last active
November 22, 2022 23:53
-
-
Save pom421/86c5d7ca39df98d402ab5cf6f9c6429c to your computer and use it in GitHub Desktop.
Custom SWRConfig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SWRConfig } from "swr"; | |
import { fetcher } from "@services/apiClient"; | |
// Your fetcher need to throw error with an statusCode. | |
const MyApp = () => { | |
return ( | |
<SWRConfig | |
value={{ | |
fetcher, | |
onErrorRetry: (error, key, config, revalidate, { retryCount }) => { | |
// Never retry on 401 Unauthorized. | |
if (error.status === 401) return; | |
// Never retry on 404. | |
if (error.status === 404) return; | |
// Never retry on 403 Forbidden. | |
if (error.status === 403) return; | |
// Never retry on 422 Unprocessable Entity. | |
if (error.status === 422) return; | |
// Only retry up to 3 times. | |
if (retryCount >= 3) return; | |
// Retry after 5 seconds. | |
setTimeout(() => revalidate({ retryCount }), 5000); | |
}, | |
}} | |
> | |
<Layout> | |
<Component {...pageProps} /> | |
</Layout> | |
</SWRConfig> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment