Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): package standalone static and public assets (#1424)

Bingxi Zhao (Frank)a7037df11dbf
diff --git a/web/scripts/build.mjs b/web/scripts/build.mjs
index af4670e57..8599fdeba 100644
--- a/web/scripts/build.mjs
+++ b/web/scripts/build.mjs
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import { spawnSync } from "node:child_process";
-import { readFileSync, rmSync, writeFileSync } from "node:fs";
+import { cpSync, existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
@@ -53,6 +53,31 @@ function prepareBuildTsconfig(snapshots, distDir) {
return buildTsconfigPath;
}
+export function packageStandaloneAssets(sourceRoot, distDir) {
+ const distRoot = path.resolve(sourceRoot, distDir);
+ const standaloneRoot = path.join(distRoot, "standalone");
+ const server = path.join(standaloneRoot, "server.js");
+ const staticAssets = path.join(distRoot, "static");
+ if (!existsSync(server) || !existsSync(staticAssets)) {
+ throw new Error(
+ `Next build did not produce a complete standalone bundle in ${distRoot}`,
+ );
+ }
+
+ // Next omits these runtime assets from standalone output (#1424). Keep the
+ // active dist directory name so server.js can resolve its own client chunks.
+ const standaloneStatic = path.join(standaloneRoot, distDir, "static");
+ rmSync(standaloneStatic, { recursive: true, force: true });
+ cpSync(staticAssets, standaloneStatic, { recursive: true });
+
+ const publicAssets = path.join(sourceRoot, "public");
+ if (existsSync(publicAssets)) {
+ const standalonePublic = path.join(standaloneRoot, "public");
+ rmSync(standalonePublic, { recursive: true, force: true });
+ cpSync(publicAssets, standalonePublic, { recursive: true });
+ }
+}
+
const snapshots = generatedPaths
.filter((path) => process.env.DEEPTUTOR_BUILD_SKIP_MISSING !== "1")
.map((path) => [path, snapshot(path)]);
@@ -65,15 +90,15 @@ export { restoreAll };
if (isEntry) {
const distDir = process.env.DEEPTUTOR_NEXT_DIST_DIR || ".next";
const buildTsconfigPath = prepareBuildTsconfig(snapshots, distDir);
+ const isVercel = process.env.VERCEL === "1";
let result;
try {
copyPdfjsAssets();
// On Vercel (process.env.VERCEL === "1") do NOT force --webpack: the
// post-build validator expects Turbopack's output layout and fails with
// ENOENT routes-manifest-deterministic.json otherwise (see issue #1428).
- // The Webpack standalone bundle is only needed by the local
- // `deeptutor start` launcher, so keep the flag for local/Docker builds.
- const isVercel = process.env.VERCEL === "1";
+ // Local and Docker builds use the Webpack standalone runtime; Vercel
+ // packages its own deployment output.
const args = isVercel
? [nextBin, "build", ...process.argv.slice(2)]
: [nextBin, "build", "--webpack", ...process.argv.slice(2)];
@@ -99,5 +124,7 @@ if (isEntry) {
console.error(result.error);
process.exit(1);
}
- process.exit(result.status ?? 1);
+ const status = result.status ?? 1;
+ if (status === 0 && !isVercel) packageStandaloneAssets(webRoot, distDir);
+ process.exit(status);
}
diff --git a/web/tests/build-wrapper.test.ts b/web/tests/build-wrapper.test.ts
index 0715b6d9b..9bdc39594 100644
--- a/web/tests/build-wrapper.test.ts
+++ b/web/tests/build-wrapper.test.ts
@@ -1,8 +1,17 @@
import test from "node:test";
import assert from "node:assert/strict";
-import { existsSync, readFileSync } from "node:fs";
+import {
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ writeFileSync,
+} from "node:fs";
import { spawnSync } from "node:child_process";
+import { tmpdir } from "node:os";
import path from "node:path";
+import { pathToFileURL } from "node:url";
const webRoot = process.cwd();
const read = (...parts: string[]) =>
@@ -112,6 +121,54 @@ test("the build wrapper restores every generated checked-in input", () => {
);
});
+test("a standalone build carries client chunks and public assets with a custom dist directory", () => {
+ const fixture = mkdtempSync(path.join(tmpdir(), "deeptutor-standalone-"));
+ const distDir = ".next-standalone-repro";
+ const distRoot = path.join(fixture, distDir);
+ const standalone = path.join(distRoot, "standalone");
+ try {
+ mkdirSync(path.join(distRoot, "static", "chunks"), { recursive: true });
+ mkdirSync(path.join(fixture, "public", "images"), { recursive: true });
+ mkdirSync(standalone, { recursive: true });
+ writeFileSync(path.join(standalone, "server.js"), "// server");
+ writeFileSync(path.join(distRoot, "static", "chunks", "app.js"), "// client");
+ writeFileSync(path.join(fixture, "public", "images", "logo.svg"), "<svg />");
+
+ const moduleUrl = pathToFileURL(
+ path.join(webRoot, "scripts", "build.mjs"),
+ ).href;
+ const result = spawnSync(
+ process.execPath,
+ [
+ "--input-type=module",
+ "-e",
+ `import { packageStandaloneAssets } from ${JSON.stringify(moduleUrl)};
+packageStandaloneAssets(process.argv[1], process.argv[2]);`,
+ fixture,
+ distDir,
+ ],
+ { cwd: webRoot, encoding: "utf8" },
+ );
+ assert.equal(result.status, 0, result.stderr);
+ assert.equal(
+ readFileSync(
+ path.join(standalone, distDir, "static", "chunks", "app.js"),
+ "utf8",
+ ),
+ "// client",
+ );
+ assert.equal(
+ readFileSync(
+ path.join(standalone, "public", "images", "logo.svg"),
+ "utf8",
+ ),
+ "<svg />",
+ );
+ } finally {
+ rmSync(fixture, { recursive: true, force: true });
+ }
+});
+
test("the standalone bundle is rooted where the Python launcher expects it", () => {
const source = read("next.config.js");
assert.match(source, /output:\s*"standalone"/);