Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix: sanitize hardcoded build-machine paths in standalone output (#147)

diegosouzapwfb2351ffe7b5
diff --git a/scripts/prepublish.mjs b/scripts/prepublish.mjs
index 55b5ec1075..381181e41e 100644
--- a/scripts/prepublish.mjs
+++ b/scripts/prepublish.mjs
@@ -51,6 +51,38 @@ console.log(" 📋 Copying standalone build to app/...");
mkdirSync(APP_DIR, { recursive: true });
cpSync(standaloneDir, APP_DIR, { recursive: true });
+// ── Step 5.5: Sanitize hardcoded build-machine paths ───────
+// Next.js standalone bakes absolute build-time paths into server.js and
+// required-server-files.json (outputFileTracingRoot, appDir, turbopack root).
+// Replace the build machine's absolute path with "." (current directory)
+// so paths resolve relative to wherever the standalone app/ is installed.
+console.log(" 🧹 Sanitizing build-machine paths...");
+const buildRoot = ROOT.replace(/\\/g, "/"); // normalise for regex safety
+const sanitizeTargets = [
+ join(APP_DIR, "server.js"),
+ join(APP_DIR, ".next", "required-server-files.json"),
+];
+let sanitisedCount = 0;
+for (const filePath of sanitizeTargets) {
+ if (!existsSync(filePath)) continue;
+ let content = readFileSync(filePath, "utf8");
+ // Escape special regex characters in the path
+ const escaped = buildRoot.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ const re = new RegExp(escaped, "g");
+ const matches = content.match(re);
+ if (matches) {
+ // Replace with "." so Next.js resolves paths relative to the standalone dir
+ content = content.replace(re, ".");
+ writeFileSync(filePath, content);
+ sanitisedCount += matches.length;
+ }
+}
+if (sanitisedCount > 0) {
+ console.log(` ✅ Sanitised ${sanitisedCount} hardcoded path references`);
+} else {
+ console.log(" ℹ️ No hardcoded paths found to sanitise");
+}
+
// ── Step 6: Copy static assets ─────────────────────────────
const staticSrc = join(ROOT, ".next", "static");
const staticDest = join(APP_DIR, ".next", "static");