From b348f30eeaf0d67566a2e852356964f945b88a97 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 12 Oct 2024 15:02:00 -0500 Subject: [PATCH] queryClient: retry certain 5xx codes automatically --- src/queries/client.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/queries/client.ts b/src/queries/client.ts index e11ce296c..0755aca5c 100644 --- a/src/queries/client.ts +++ b/src/queries/client.ts @@ -1,12 +1,30 @@ import { QueryClient } from '@tanstack/react-query'; +import { HTTPError } from 'soapbox/api/HTTPError'; + +/** HTTP response codes to retry. */ +const RETRY_CODES = [502, 503, 504, 521, 522]; + const queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false, staleTime: 60000, // 1 minute gcTime: Infinity, - retry: false, + retry(failureCount: number, error: Error): boolean { + if (error instanceof HTTPError) { + const { response } = error; + + // TODO: Implement Retry-After. + // const retryAfter = response.headers.get('Retry-After'); + + if (RETRY_CODES.includes(response.status)) { + return failureCount < 3; + } + } + + return false; + }, }, }, });