Add network retries and ensure only one attempt
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user