Skip to content

Commit

Permalink
feat: use ofetch retryDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebilicious committed Aug 8, 2023
1 parent 344180a commit 6d7f8b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
23 changes: 12 additions & 11 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -163,16 +166,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<Response>(
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<ReturnType<typeof fetch>>;

retry: nitro.options.prerender.retries ?? 2, // By default we retry 2 times
retryDelay: nitro.options.prerender.retryDelay ?? 250,

Check failure on line 174 in src/prerender.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

Argument of type '{ headers: { "x-nitro-prerender": string; }; retry: number; retryDelay: number; }' is not assignable to parameter of type 'FetchOptions<"json">'.
}
);
// Data will be removed as soon as written to the disk
let dataBuff: Buffer | undefined = Buffer.from(await res.arrayBuffer());

Expand Down
27 changes: 0 additions & 27 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
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;
}
}

0 comments on commit 6d7f8b8

Please sign in to comment.