0.2 在线版本打通
This commit is contained in:
+56
-5
@@ -157,8 +157,35 @@ function requireFile(filePath, hint) {
|
||||
}
|
||||
}
|
||||
|
||||
function ensureStandaloneStatic(frontendDir) {
|
||||
// 说明:当 next.config.ts 配置了 output: "standalone" 时,推荐使用
|
||||
// `node .next/standalone/server.js` 启动。
|
||||
// 但 Next 不会自动把 `.next/static` 放入 `.next/standalone/.next/static`,
|
||||
// 若缺失会导致 `/_next/static/*` 404(远程访问尤其明显)。
|
||||
const standaloneServer = path.join(frontendDir, ".next", "standalone", "server.js");
|
||||
if (!fs.existsSync(standaloneServer)) return false;
|
||||
|
||||
const src = path.join(frontendDir, ".next", "static");
|
||||
const dest = path.join(frontendDir, ".next", "standalone", ".next", "static");
|
||||
if (!fs.existsSync(src)) return true;
|
||||
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
||||
// Node 16+:fs.cpSync
|
||||
fs.cpSync(src, dest, { recursive: true });
|
||||
return true;
|
||||
} catch (err) {
|
||||
logPrefix(
|
||||
"frontend",
|
||||
`复制 .next/static 到 standalone 失败:${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
// 失败时不强制退出,仍尝试启动(方便你自行排查)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const frontendProdEnv = path.join(frontendDir, ".env.production.local");
|
||||
const frontendProdEnv = path.join(frontendDir, ".env.production.local");
|
||||
|
||||
// 说明:Next.js 在 production 也会加载 wolai-frontend/.env.local。
|
||||
// 为避免被本地开发环境(可能指向远端 Supabase)的配置污染,这里把“服务端专用”的关键 env 显式注入。
|
||||
@@ -181,6 +208,21 @@ async function main() {
|
||||
NEXT_TELEMETRY_DISABLED: "1",
|
||||
};
|
||||
|
||||
// Next 的 API Route 也会用到这些“内部服务”环境变量(Search / MinerU / LightRAG 等)。
|
||||
// 规则:从仓库根目录 .env.local/.env 读取并注入,避免 production server 缺少配置。
|
||||
const passthroughKeys = [
|
||||
"MINERU_ENDPOINT",
|
||||
"LIGHTRAG_URL",
|
||||
"LIGHTRAG_API_KEY",
|
||||
"SEARXNG_BASE_URL",
|
||||
"SEARXNG_API_TOKEN",
|
||||
];
|
||||
for (const key of passthroughKeys) {
|
||||
if (envRoot[key] && !envFrontend[key]) {
|
||||
envFrontend[key] = envRoot[key];
|
||||
}
|
||||
}
|
||||
|
||||
// Next 服务端(API Route)需要 service role 才能生成签名 URL、清理资源等。
|
||||
if (envRoot.SUPABASE_SERVICE_ROLE_KEY) {
|
||||
envFrontend.SUPABASE_SERVICE_ROLE_KEY = envRoot.SUPABASE_SERVICE_ROLE_KEY;
|
||||
@@ -225,6 +267,8 @@ async function main() {
|
||||
logPrefix("frontend", "已跳过 build(SKIP_FRONTEND_BUILD=1)");
|
||||
}
|
||||
|
||||
const useStandalone = ensureStandaloneStatic(frontendDir);
|
||||
|
||||
// 预检查:提醒必要端口是否已就绪(不强制退出,避免误伤已有进程)
|
||||
const portChecks = [
|
||||
{ name: "frontend", port: frontendPort },
|
||||
@@ -244,12 +288,19 @@ async function main() {
|
||||
}
|
||||
|
||||
// 启动前端(production)
|
||||
const envFrontendServer = {
|
||||
...envFrontend,
|
||||
PORT: String(frontendPort),
|
||||
HOSTNAME: "0.0.0.0",
|
||||
};
|
||||
spawnTask({
|
||||
name: "frontend",
|
||||
// Windows 下 pnpm 对 `start -- ...` 的参数转发偶发不稳定;用 `pnpm exec next start` 更可靠。
|
||||
command: `pnpm -C wolai-frontend exec next start -p ${frontendPort} -H 0.0.0.0`,
|
||||
cwd: rootDir,
|
||||
env: envFrontend,
|
||||
// 说明:next.config.ts 配置了 output: "standalone" 时,应使用 standalone server 启动,避免 next start 行为不一致。
|
||||
command: useStandalone
|
||||
? "node .next/standalone/server.js"
|
||||
: `pnpm -C wolai-frontend exec next start -p ${frontendPort} -H 0.0.0.0`,
|
||||
cwd: useStandalone ? frontendDir : rootDir,
|
||||
env: envFrontendServer,
|
||||
});
|
||||
|
||||
// 启动 wolai-backend(FastAPI)
|
||||
|
||||
Reference in New Issue
Block a user