Skip to content

Instantly share code, notes, and snippets.

@pom421
Last active November 22, 2022 23:53
Show Gist options
  • Save pom421/86c5d7ca39df98d402ab5cf6f9c6429c to your computer and use it in GitHub Desktop.
Save pom421/86c5d7ca39df98d402ab5cf6f9c6429c to your computer and use it in GitHub Desktop.
Custom SWRConfig
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