diff --git a/src/lib/async/retry.ts b/src/lib/async/retry.ts index 8a1729091d..479a1cdc12 100644 --- a/src/lib/async/retry.ts +++ b/src/lib/async/retry.ts @@ -29,6 +29,7 @@ export async function retry

( export async function networkRetry

( retries: number, fn: () => Promise

, + delay?: number, ): Promise

{ - return retry(retries, isNetworkError, fn) + return retry(retries, isNetworkError, fn, delay) } diff --git a/src/state/appConfig.tsx b/src/state/appConfig.tsx index 2258efe3ad..6f5b0096c8 100644 --- a/src/state/appConfig.tsx +++ b/src/state/appConfig.tsx @@ -1,6 +1,7 @@ import {createContext, useContext} from 'react' import {QueryClient, useQuery} from '@tanstack/react-query' +import {networkRetry} from '#/lib/async/retry' import {APP_CONFIG_URL} from '#/env' const qc = new QueryClient() @@ -26,9 +27,22 @@ export const DEFAULT_APP_CONFIG_RESPONSE: AppConfigResponse = { }, } +let fetchAppConfigPromise: Promise + async function fetchAppConfig(): Promise { try { - const res = await fetch(`${APP_CONFIG_URL}/config`) + if (!fetchAppConfigPromise) { + fetchAppConfigPromise = networkRetry( + 3, + async () => { + const r = await fetch(`${APP_CONFIG_URL}/config`) + if (!r.ok) throw new Error(await r.text()) + return r + }, + 1e3, + ) + } + const res = await fetchAppConfigPromise if (!res.ok) return null const data = await res.json() return data