Add network retries and ensure only one attempt

This commit is contained in:
Eric Bailey
2026-02-13 14:39:12 -06:00
parent e8b5f955ad
commit 4e89ffc790
2 changed files with 17 additions and 2 deletions
+2 -1
View File
@@ -29,6 +29,7 @@ export async function retry<P>(
export async function networkRetry<P>(
retries: number,
fn: () => Promise<P>,
delay?: number,
): Promise<P> {
return retry(retries, isNetworkError, fn)
return retry(retries, isNetworkError, fn, delay)
}
+15 -1
View File
@@ -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<Response>
async function fetchAppConfig(): Promise<AppConfigResponse | null> {
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