Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prerender): support retry and retryDelay #1534

Merged
merged 15 commits into from
Oct 5, 2023
6 changes: 5 additions & 1 deletion docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ Default:
failOnError: false,
crawlLinks: false,
ignore: [],
routes: []
routes: [],
retries: 3,
retryDelay: 500
}
```

Expand All @@ -372,6 +374,8 @@ Set `autoSubfolderIndex` lets you control how to generate the files in the `.out

This option is useful when your hosting provider does not give you an option regarding the trailing slash.

The prerenderer will attempt to render pages 3 times with a delay of 500ms. Use `retry` and `retryDelay` to change this behavior.

## Directories

### `rootDir`
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const NitroDefaults: NitroConfig = {
autoSubfolderIndex: true,
concurrency: 1,
interval: 0,
retry: 3,
retryDelay: 500,
failOnError: false,
crawlLinks: false,
ignore: [],
Expand Down
13 changes: 9 additions & 4 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from "chalk";
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
import { defu } from "defu";
import mime from "mime";
import type { $Fetch } from "ofetch";
import { createNitro } from "./nitro";
import { build } from "./build";
import type { Nitro, NitroRouteRules, PrerenderRoute } from "./types";
Expand Down Expand Up @@ -77,7 +78,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 @@ -164,13 +167,15 @@ export async function prerender(nitro: Nitro) {

// Fetch the route
const encodedRoute = encodeURI(route);
const res = await (localFetch(

const res = await localFetch<Response>(
withBase(encodedRoute, nitro.options.baseURL),
{
headers: { "x-nitro-prerender": encodedRoute },
retry: nitro.options.prerender.retry,
retryDelay: nitro.options.prerender.retryDelay,
}
) as ReturnType<typeof fetch>);

);
// Data will be removed as soon as written to the disk
let dataBuff: Buffer | undefined = Buffer.from(await res.arrayBuffer());

Expand Down
10 changes: 10 additions & 0 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ export interface NitroOptions extends PresetOptions {
failOnError: boolean;
ignore: string[];
routes: string[];
/**
* Amount of retries. Pass Infinity to retry indefinitely.
* @default 3
*/
retry: number;
/**
* Delay between each retry in ms.
* @default 500
*/
retryDelay: number;
};

// Rollup
Expand Down