With output: "standalone", next build writes server.js under .next/standalone but leaves generated static/public assets in the project root, so starting the standalone server directly (e.g. via PM2) 404s on JS/CSS/font/favicon requests and /login stays stuck loading. Add a postbuild step that copies .next/static and public into the standalone directory, skipping the workspace-traced CLI build which already copies its own assets.
37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
import { cpSync, existsSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import { dirname, resolve } from "node:path";
|
|
|
|
export function copyStandaloneAssets({ projectRoot = process.cwd(), distDir = process.env.NEXT_DIST_DIR || ".next" } = {}) {
|
|
if (process.env.NEXT_TRACING_ROOT_MODE === "workspace") {
|
|
console.log("[standalone-assets] Skipping workspace-traced CLI build; CLI packaging handles assets");
|
|
return;
|
|
}
|
|
|
|
const buildDir = resolve(projectRoot, distDir);
|
|
const standaloneDir = resolve(buildDir, "standalone");
|
|
|
|
if (!existsSync(standaloneDir)) {
|
|
console.log(`[standalone-assets] No standalone build found at ${standaloneDir}`);
|
|
return;
|
|
}
|
|
|
|
const staticSource = resolve(buildDir, "static");
|
|
const staticDestination = resolve(standaloneDir, distDir, "static");
|
|
if (existsSync(staticSource)) {
|
|
cpSync(staticSource, staticDestination, { recursive: true, force: true });
|
|
console.log(`[standalone-assets] Copied static assets to ${staticDestination}`);
|
|
}
|
|
|
|
const publicSource = resolve(projectRoot, "public");
|
|
const publicDestination = resolve(standaloneDir, "public");
|
|
if (existsSync(publicSource)) {
|
|
cpSync(publicSource, publicDestination, { recursive: true, force: true });
|
|
console.log(`[standalone-assets] Copied public assets to ${publicDestination}`);
|
|
}
|
|
}
|
|
|
|
if (process.argv[1] && resolve(process.argv[1]) === resolve(dirname(fileURLToPath(import.meta.url)), "copy-standalone-assets.mjs")) {
|
|
copyStandaloneAssets();
|
|
}
|