CódigoNão tentada
Tentar de novo um import lazy
Dificuldade: média
Envolva um import() dinâmico para que um carregamento que falhou seja repetido com backoff exponencial, e o último erro seja relançado quando todas as tentativas falharem. Depois explique por que isso ajuda com redes instáveis, mas não com um chunk apagado por um deploy.
Ver solução de referência
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
export async function importWithRetry<T>( load: () => Promise<T>, { retries = 2, baseDelayMs = 500, wait = sleep }: { retries?: number; baseDelayMs?: number; wait?: (ms: number) => Promise<void> } = {},): Promise<T> { for (let attempt = 0; ; attempt++) { try { return await load(); } catch (error) { // Out of retries: let the caller (an error handler that reloads, or an error boundary) deal with it. if (attempt >= retries) throw error; await wait(baseDelayMs * 2 ** attempt); } }}
Ctrl Enter