Compare commits

...

6 Commits

Author SHA1 Message Date
dvoytenko
3f29798a5a strip to .next, report more failures 2024-01-31 16:17:45 +01:00
dvoytenko
5358a00611 Use absolute paths for require 2024-01-31 16:17:45 +01:00
Javi Velasco
70db54f59b Add measures 2024-01-31 16:17:45 +01:00
Javi Velasco
aafdd66b79 Use require 2024-01-31 16:17:45 +01:00
Javi Velasco
ca59387aac Add common and rest of chunks to the server launcher template 2024-01-31 16:17:45 +01:00
Javi Velasco
fb5eac2fbc Add logic to add common and rest of chunks 2024-01-31 16:17:45 +01:00
2 changed files with 113 additions and 2 deletions

View File

@@ -1001,12 +1001,54 @@ export async function serverBuild({
for (const group of combinedGroups) {
const groupPageFiles: { [key: string]: PseudoFile } = {};
const filesCounter = new Map<string, number>();
const filesMap = new Map<string, string>();
for (const page of [...group.pages, ...internalPages]) {
const pageFileName = path.normalize(
path.relative(baseDir, lambdaPages[page].fsPath)
);
groupPageFiles[pageFileName] = compressedPages[page];
const traceFileRef = getBuildTraceFile(getOriginalPagePath(page));
if (traceFileRef && !internalPages.includes(page)) {
const traceFile = await fs.readFile(traceFileRef.fsPath, 'utf8');
const { files } = JSON.parse(traceFile) as { files: string[] };
const pagePath = path.join(
appDir && Boolean(lambdaAppPaths[page]) ? appDir : pagesDir,
getOriginalPagePath(page)
);
const pageDir = path.dirname(pagePath);
filesCounter.set(pagePath, 1);
let relPath0 = `./${path.relative(path.resolve(baseDir), pagePath)}`;
if (relPath0.startsWith('./apps/vercel-site/')) {
relPath0 = relPath0.replace('./apps/vercel-site/', './');
}
filesMap.set(pagePath, relPath0);
// filesMap.set(pagePath, pagePath);
files.forEach((file: string) => {
const absolutePath = path.join(pageDir, file);
const count = filesCounter.get(absolutePath) || 0;
filesCounter.set(absolutePath, count + 1);
let relPath = path.relative(
path.resolve(baseDir),
path.resolve(absolutePath)
);
if (!relPath.startsWith('..')) {
relPath = './' + relPath;
}
if (relPath.startsWith('./apps/vercel-site/')) {
relPath = relPath.replace('./apps/vercel-site/', './');
}
filesMap.set(absolutePath, relPath);
// filesMap.set(absolutePath, absolutePath);
});
}
}
const updatedManifestFiles: { [name: string]: FileBlob } = {};
@@ -1068,9 +1110,40 @@ export async function serverBuild({
}
}
const commonFiles = new Set<string>();
const restFiles = new Set<string>();
for (const [file, count] of filesCounter) {
const relative = filesMap.get(file)!;
if (
file.endsWith('.js') ||
file.endsWith('.mjs') ||
file.endsWith('.cjs')
) {
if (count === group.pages.length) {
commonFiles.add(relative);
} else {
restFiles.add(relative);
}
}
}
let launcherContent = group.isAppRouter ? appLauncher : launcher;
launcherContent = launcherContent.replace(
'const commonChunks = __COMMON_CHUNKS__',
`const commonChunks = ${JSON.stringify(
Array.from(commonFiles.values())
)};`
);
launcherContent = launcherContent.replace(
'const restChunks = __REST_CHUNKS__',
`const restChunks = ${JSON.stringify(Array.from(restFiles.values()))};`
);
const launcherFiles: { [name: string]: FileFsRef | FileBlob } = {
[path.join(path.relative(baseDir, projectDir), '___next_launcher.cjs')]:
new FileBlob({ data: group.isAppRouter ? appLauncher : launcher }),
new FileBlob({ data: launcherContent }),
};
const operationType = getOperationType({ group, prerenderManifest });

View File

@@ -1,4 +1,4 @@
import { IncomingMessage, ServerResponse } from 'http';
import type { IncomingMessage, ServerResponse } from 'http';
// The Next.js builder can emit the project in a subdirectory depending on how
// many folder levels of `node_modules` are traced. To ensure `process.cwd()`
// returns the proper path, we change the directory to the folder with the
@@ -53,6 +53,44 @@ const serve =
// The default handler method should be exported as a function on the module.
module.exports = serve(nextServer.getRequestHandler());
declare const __COMMON_CHUNKS__: string[];
const commonChunks = __COMMON_CHUNKS__;
module.exports.preload = async () => {
const start = performance.now();
const failed: string[] = [];
for (const chunk of commonChunks) {
try {
require(chunk);
} catch (e) {
failed.push(e.message);
}
}
performance.measure('vc:common-chunks', {
detail: 'Next.js common chunks',
end: performance.now(),
start,
});
if (failed.length > 0) {
throw new Error(`Failed to preload chunks: ${failed.length}: ${failed.join(', ')}`);
}
};
declare const __REST_CHUNKS__: string[];
const restChunks = __REST_CHUNKS__;
module.exports.postload = async () => {
const failed: string[] = [];
for (const chunk of restChunks) {
try {
require(chunk);
} catch (e) {
failed.push(e.message);
}
}
if (failed.length > 0) {
throw new Error(`Failed to postload chunks: ${failed.length}: ${failed.join(', ')}`);
}
};
// If available, add `getRequestHandlerWithMetadata` to the export if it's
// required by the configuration.
if (