From 4669c0a919a10e2fd8a2bafd94f6981800028c00 Mon Sep 17 00:00:00 2001 From: Hebilicious Date: Tue, 8 Aug 2023 20:33:08 +0700 Subject: [PATCH] feat: use ofetch retryDelay --- src/prerender.ts | 23 ++++++++++++----------- src/utils/index.ts | 27 --------------------------- 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/src/prerender.ts b/src/prerender.ts index 998eec353f..0d112fdb88 100644 --- a/src/prerender.ts +++ b/src/prerender.ts @@ -4,10 +4,11 @@ import { joinURL, parseURL, withBase, withoutBase } from "ufo"; import chalk from "chalk"; import { createRouter as createRadixRouter, toRouteMatcher } from "radix3"; import { defu } from "defu"; +import type { $Fetch } from "ofetch"; import { createNitro } from "./nitro"; import { build } from "./build"; import type { Nitro, NitroRouteRules, PrerenderRoute } from "./types"; -import { localFetchWithRetries, writeFile } from "./utils"; +import { writeFile } from "./utils"; import { compressPublicAssets } from "./compress"; const allowedExtensions = new Set(["", ".json"]); @@ -75,7 +76,9 @@ export async function prerender(nitro: Nitro) { nitroRenderer.options.output.serverDir, "index.mjs" ); - const { localFetch } = await import(pathToFileURL(serverEntrypoint).href); + const { localFetch } = (await import( + pathToFileURL(serverEntrypoint).href + )) as { localFetch: $Fetch }; // Create route rule matcher const _routeRulesMatcher = toRouteMatcher( @@ -153,16 +156,14 @@ export async function prerender(nitro: Nitro) { // Fetch the route const encodedRoute = encodeURI(route); - const res = (await localFetchWithRetries({ - url: withBase(encodedRoute, nitro.options.baseURL), - options: { + const res = await localFetch( + withBase(encodedRoute, nitro.options.baseURL), + { headers: { "x-nitro-prerender": encodedRoute }, - }, - fetcher: localFetch, - retries: nitro.options.prerender.retries ?? 2, // By default we retry 2 times - delay: nitro.options.prerender.retryDelay ?? 500, - })) as Awaited>; - + retry: nitro.options.prerender.retries ?? 2, // By default we retry 2 times + retryDelay: nitro.options.prerender.retryDelay ?? 250, + } + ); // Data will be removed as soon as written to the disk let dataBuff: Buffer | undefined = Buffer.from(await res.arrayBuffer()); diff --git a/src/utils/index.ts b/src/utils/index.ts index 574a1d8894..a0c0e25fda 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -240,30 +240,3 @@ type FetchWithRetries = { /** Delay between each retry in ms. */ delay?: number; }; -export async function fetchWithRetries({ - url, - options, - fetcher = fetch, - retries = 3, - delay = 1000, -}: FetchWithRetries): Promise { - try { - const res = await fetcher(url, options); - if (!res.ok) { - throw new Error(res.statusText); - } - return res; - } catch (error) { - if (retries > 0) { - // wait 1s before retrying - await new Promise((resolve) => setTimeout(resolve, delay)); - return fetchWithRetries({ - url, - options, - fetcher, - retries: retries - 1, - }); - } - throw error; - } -}