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: add prerender:config, prerender:init and prerender:done hooks #1519

Merged
merged 28 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4439b11
feat: new hook `prerender:done`
harlan-zw Aug 1, 2023
a9767e8
chore: lint
harlan-zw Aug 1, 2023
876e2b9
chore: add all prerenderer hooks
harlan-zw Aug 1, 2023
37814b3
chore: refactor
harlan-zw Aug 1, 2023
7b6750a
chore: fix tests
harlan-zw Aug 1, 2023
ae4d498
chore: apply automated lint fixes
autofix-ci[bot] Aug 1, 2023
49c6531
chore: allow config overrides
harlan-zw Aug 1, 2023
242729d
Merge remote-tracking branch 'origin/feat/prerender-done-hook' into f…
harlan-zw Aug 1, 2023
32df663
chore: apply automated lint fixes
autofix-ci[bot] Aug 1, 2023
f64bd7d
chore: artifact
harlan-zw Aug 1, 2023
2942a87
Merge remote-tracking branch 'origin/feat/prerender-done-hook' into f…
harlan-zw Aug 1, 2023
a7b616e
chore: fix tests
harlan-zw Aug 1, 2023
7f8784b
chore: expose all config to hook
harlan-zw Aug 2, 2023
796ffe1
Merge branch 'main' of github.com:unjs/nitro into feat/prerender-done…
harlan-zw Aug 2, 2023
ecbcd35
chore: prefer `prerenderer:close`
harlan-zw Aug 2, 2023
aa587b5
chore: prefer `prerenderer:close`
harlan-zw Aug 2, 2023
6df01a1
chore: prefer `prerenderer:done`
harlan-zw Aug 2, 2023
853b0d7
chore: simpler `prerenderer:done` hook
harlan-zw Aug 2, 2023
9406a66
chore: update test scope
harlan-zw Aug 2, 2023
2adb30f
chore: update snapshot
harlan-zw Aug 2, 2023
e74c4b0
chore: remove tests
harlan-zw Aug 2, 2023
5bcd60a
Merge branch 'main' into feat/prerender-done-hook
pi0 Aug 6, 2023
2ef760d
refactor: rename `prerenderer:` to `prerender:`
pi0 Aug 6, 2023
f677ad0
refactor: pass result context as first arg to `prerender:done` hook
pi0 Aug 6, 2023
b1e4a0d
refactor: sort hooks
pi0 Aug 6, 2023
7e813ba
fix types to use `PrerenderRoute`
pi0 Aug 6, 2023
3d43e50
refactor: deprecate PrerenderGenerateRoute to avoid future alike issues
pi0 Aug 6, 2023
1e638bc
refactor: consistant types + more generic `fail`ed routes
pi0 Aug 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { defu } from "defu";
import { createNitro } from "./nitro";
import { build } from "./build";
import type { Nitro, NitroRouteRules, PrerenderGenerateRoute } from "./types";
import type { Nitro, NitroRouteRules, PrerenderRoute } from "./types";
import { writeFile } from "./utils";
import { compressPublicAssets } from "./compress";

Expand Down Expand Up @@ -49,13 +49,16 @@
// Build with prerender preset
nitro.logger.info("Initializing prerenderer");
nitro._prerenderedRoutes = [];
const nitroRenderer = await createNitro({
const prerendererConfig = {
...nitro.options._config,
static: false,
rootDir: nitro.options.rootDir,
logLevel: 0,
preset: "nitro-prerender",
});
};
await nitro.hooks.callHook("prerender:config", prerendererConfig);
const nitroRenderer = await createNitro(prerendererConfig);
await nitro.hooks.callHook("prerender:init", nitroRenderer);

// Set path to preview prerendered routes relative to the "host" nitro preset
let path = relative(nitro.options.output.dir, nitro.options.output.publicDir);
Expand Down Expand Up @@ -83,7 +86,7 @@

// Start prerendering
const generatedRoutes = new Set();
const erroredRoutes = new Set<PrerenderGenerateRoute>();
const failedRoutes = new Set<PrerenderRoute>();
const skippedRoutes = new Set();
const displayedLengthWarns = new Set();
const canPrerender = (route = "/") => {
Expand Down Expand Up @@ -145,7 +148,7 @@
generatedRoutes.add(route);

// Create result object
const _route: PrerenderGenerateRoute = { route };
const _route: PrerenderRoute & { skip?: boolean } = { route };

// Fetch the route
const encodedRoute = encodeURI(route);
Expand Down Expand Up @@ -188,7 +191,7 @@
_route.error = new Error(`[${res.status}] ${res.statusText}`) as any;
_route.error.statusCode = res.status;
_route.error.statusMessage = res.statusText;
erroredRoutes.add(_route);
failedRoutes.add(_route);
}

// Write to the file
Expand Down Expand Up @@ -254,9 +257,14 @@
interval: nitro.options.prerender.interval,
});

if (nitro.options.prerender.failOnError && erroredRoutes.size > 0) {
await nitro.hooks.callHook("prerender:done", {
prerenderedRoutes: nitro._prerenderedRoutes,
failedRoutes: [...failedRoutes],
});

if (nitro.options.prerender.failOnError && failedRoutes.size > 0) {
nitro.logger.log("\nErrors prerendering:");
for (const route of erroredRoutes) {
for (const route of failedRoutes) {

Check warning on line 267 in src/prerender.ts

View check run for this annotation

Codecov / codecov/patch

src/prerender.ts#L267

Added line #L267 was not covered by tests
const parents = linkParents.get(route.route);
const parentsText = parents?.size
? `\n${[...parents.values()]
Expand Down Expand Up @@ -370,7 +378,7 @@
return (pathname.match(EXT_REGEX) || [])[0] || "";
}

function formatPrerenderRoute(route: PrerenderGenerateRoute) {
function formatPrerenderRoute(route: PrerenderRoute) {
let str = ` β”œβ”€ ${route.route} (${route.generateTimeMS}ms)`;

if (route.error) {
Expand Down
14 changes: 11 additions & 3 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface Nitro {
updateConfig: (config: NitroDynamicConfig) => void | Promise<void>;

/* @internal */
_prerenderedRoutes?: PrerenderGenerateRoute[];
_prerenderedRoutes?: PrerenderRoute[];
}

export interface PrerenderRoute {
Expand All @@ -71,6 +71,7 @@ export interface PrerenderRoute {
generateTimeMS?: number;
}

/** @deprecated Internal type will be removed in future versions */
export interface PrerenderGenerateRoute extends PrerenderRoute {
skip?: boolean;
}
Expand All @@ -83,12 +84,19 @@ export interface NitroHooks {
"rollup:reload": () => HookResult;
restart: () => HookResult;
close: () => HookResult;
// Prerender
"prerender:routes": (routes: Set<string>) => HookResult;
"prerender:route": (route: PrerenderRoute) => HookResult;
"prerender:config": (config: NitroConfig) => HookResult;
"prerender:init": (prerenderer: Nitro) => HookResult;
"prerender:generate": (
route: PrerenderGenerateRoute,
route: PrerenderRoute & { skip?: boolean },
nitro: Nitro
) => HookResult;
"prerender:route": (route: PrerenderRoute) => HookResult;
"prerender:done": (result: {
prerenderedRoutes: PrerenderRoute[];
failedRoutes: PrerenderRoute[];
}) => HookResult;
}

type CustomDriverName = string & { _custom?: any };
Expand Down