fix(build): include assets in standalone output

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.
This commit is contained in:
DaDecky
2026-08-05 10:31:34 +07:00
committed by decolua
parent 0648e9e420
commit 786b3013ba
3 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
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();
}