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

fix(prerender): check each segment length is less than 255 chars and whole path 1024 #757

Merged
merged 6 commits into from
Dec 15, 2022
Merged
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
35 changes: 33 additions & 2 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,52 @@ export async function prerender(nitro: Nitro) {

// Start prerendering
const generatedRoutes = new Set();
const displayedLengthWarns = new Set();
const canPrerender = (route: string = "/") => {
// Skip if route is already generated
if (generatedRoutes.has(route)) {
return false;
}
if (route.length > 250) {
return false;

// Ensure length is not too long for filesystem
// https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
const FS_MAX_SEGMENT = 255;
// 1024 is the max path length on APFS (undocumented)
const FS_MAX_PATH = 1024;
const FS_MAX_PATH_PUBLIC_HTML =
FS_MAX_PATH - (nitro.options.output.publicDir.length + 10);

if (
(route.length >= FS_MAX_PATH_PUBLIC_HTML ||
route.split("/").some((s) => s.length > FS_MAX_SEGMENT)) &&
!displayedLengthWarns.has(route)
) {
displayedLengthWarns.add(route);
const _route = route.slice(0, 60) + "...";
if (route.length >= FS_MAX_PATH_PUBLIC_HTML) {
nitro.logger.warn(
`Prerendering long route "${_route}" (${route.length}) can cause filesystem issues since it exceeds ${FS_MAX_PATH_PUBLIC_HTML}-character limit when writing to \`${nitro.options.output.publicDir}\`.`
);
} else {
nitro.logger.warn(
`Skipping prerender of the route "${_route}" since it exceeds the ${FS_MAX_SEGMENT}-character limit in one of the path segments and can cause filesystem issues.`
);
return false;
}
}

// Check for explicitly ignored routes
for (const ignore of nitro.options.prerender.ignore) {
if (route.startsWith(ignore)) {
return false;
}
}

// Check for route rules explicitly disabling prerender
if (_getRouteRules(route).prerender === false) {
return false;
}

return true;
};

Expand Down