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>(
|
export async function networkRetry<P>(
|
||||||
retries: number,
|
retries: number,
|
||||||
fn: () => Promise<P>,
|
fn: () => Promise<P>,
|
||||||
|
delay?: number,
|
||||||
): Promise<P> {
|
): 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 {createContext, useContext} from 'react'
|
||||||
import {QueryClient, useQuery} from '@tanstack/react-query'
|
import {QueryClient, useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {networkRetry} from '#/lib/async/retry'
|
||||||
import {APP_CONFIG_URL} from '#/env'
|
import {APP_CONFIG_URL} from '#/env'
|
||||||
|
|
||||||
const qc = new QueryClient()
|
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> {
|
async function fetchAppConfig(): Promise<AppConfigResponse | null> {
|
||||||
try {
|
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
|
if (!res.ok) return null
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
return data
|
return data
|
||||||
|
|||||||
Reference in New Issue
Block a user