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

refactor(prerender): improve console formatting for failed routes #1471

Merged
merged 8 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
56 changes: 37 additions & 19 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { compressPublicAssets } from "./compress";

const allowedExtensions = new Set(["", ".json"]);

const linkParents = new Map<string, Set<string>>();

export async function prerender(nitro: Nitro) {
if (nitro.options.noPublicDir) {
console.warn(
Expand Down Expand Up @@ -235,20 +237,7 @@ export async function prerender(nitro: Nitro) {
}

await nitro.hooks.callHook("prerender:route", _route);

if (_route.error) {
nitro.logger.log(
chalk[_route.error.statusCode === 404 ? "yellow" : "red"](
` β”œβ”€ ${_route.route} (${
_route.generateTimeMS
}ms) ${`(${_route.error})`}`
)
);
} else {
nitro.logger.log(
chalk.gray(` β”œβ”€ ${_route.route} (${_route.generateTimeMS}ms)`)
);
}
nitro.logger.log(formatPrerenderRoute(_route));
}

await runParallel(routes, processRoute, {
Expand All @@ -259,11 +248,13 @@ export async function prerender(nitro: Nitro) {
if (nitro.options.prerender.failOnError && erroredRoutes.size > 0) {
nitro.logger.log("\nErrors prerendering:");
for (const route of erroredRoutes) {
nitro.logger.log(
chalk[route.error.statusCode === 404 ? "yellow" : "red"](
` β”œβ”€ ${route.route} (${route.error.statusCode})`
)
);
const parents = linkParents.get(route.route);
const parentsText = parents?.size
? `\n${[...parents.values()]
.map((link) => chalk.gray(` β”‚ └── Linked from ${link}`))
.join("\n")}`
: "";
nitro.logger.log(formatPrerenderRoute(route));
}
nitro.logger.log("");
throw new Error("Exiting due to prerender errors.");
Expand Down Expand Up @@ -350,6 +341,14 @@ function extractLinks(
}
links.push(pathname);
}
for (const link of links) {
const _parents = linkParents.get(link);
if (_parents) {
_parents.add(from);
} else {
linkParents.set(link, new Set([from]));
}
}
return links;
}

Expand All @@ -359,3 +358,22 @@ function getExtension(link: string): string {
const pathname = parseURL(link).pathname;
return (pathname.match(EXT_REGEX) || [])[0] || "";
}

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

if (route.error) {
const parents = linkParents.get(route.route);
const errorColor = chalk[route.error.statusCode === 404 ? "yellow" : "red"];
const errorLead = parents?.size ? "β”œβ”€β”€" : "└──";
str += `\n β”‚ ${errorLead} ${errorColor(route.error)}`;

if (parents?.size) {
str += `\n${[...parents.values()]
.map((link) => ` β”‚ └── Linked from ${link}`)
.join("\n")}`;
}
}

return chalk.gray(str);
}
1 change: 1 addition & 0 deletions test/fixture/routes/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { appendHeader } from "h3";

export default defineEventHandler((event) => {
const links = [
"/404",
"https://about.google/products/",
"/api/hello",
"/api/hello?bar=baz",
Expand Down