From 4e9f815a53fe8f8319ec88b674bcf5adbb2fb3a6 Mon Sep 17 00:00:00 2001 From: renato97 Date: Thu, 23 Jul 2026 16:25:52 -0300 Subject: [PATCH] fix: standalone start script copies static files automatically - scripts/start.js: copies .next/static to .next/standalone/.next/static before launching the standalone server (prevents CSS 404) - package.json: start script now uses scripts/start.js - Dockerfile: removed fragile manual better-sqlite3 copy (already included in standalone output) - scripts/setup.sh: added local prod option for npm start --- Dockerfile | 6 ++---- package.json | 2 +- scripts/setup.sh | 21 +++++++++++++++------ scripts/start.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 scripts/start.js diff --git a/Dockerfile b/Dockerfile index 5876947..05dba1f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,13 +16,11 @@ ENV NODE_ENV=production RUN addgroup --system --gid 1001 nodejs && \ adduser --system --uid 1001 nextjs -RUN mkdir -p /app/data/covers && chown -R nextjs:nodejs /app/data - COPY --from=builder /app/public ./public -RUN mkdir .next COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static -COPY --from=builder /app/node_modules/better-sqlite3 ./node_modules/better-sqlite3 + +RUN mkdir -p /app/data/covers && chown -R nextjs:nodejs /app/data USER nextjs EXPOSE 3000 diff --git a/package.json b/package.json index 242d9d3..ebc0caa 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "node scripts/start.js", "lint": "eslint" }, "dependencies": { diff --git a/scripts/setup.sh b/scripts/setup.sh index 69381f4..b3281e6 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -13,22 +13,31 @@ fi echo "" echo "Choose install method:" -echo " 1) Local (npm run dev)" -echo " 2) Docker Compose" +echo " 1) Local dev (npm run dev)" +echo " 2) Local prod (npm run build && npm start)" +echo " 3) Docker Compose (producción)" echo "" -read -rp "Method [1/2]: " method +read -rp "Method [1/2/3]: " method -if [ "$method" = "2" ]; then +if [ "$method" = "3" ]; then echo "" echo "=== Docker build & start ===" docker compose up -d --build echo "" echo "Web running at http://localhost:3000" +elif [ "$method" = "2" ]; then + echo "" + echo "=== Local prod build ===" + npm install + npm run build + echo "" + echo "Run: npm start" + echo "Web running at http://localhost:3000" else echo "" - echo "=== Local install ===" + echo "=== Local dev ===" npm install echo "" echo "Run: npm run dev" - echo "Or: npm run build && npm start" + echo "Web running at http://localhost:3000" fi diff --git a/scripts/start.js b/scripts/start.js new file mode 100644 index 0000000..8ba48c0 --- /dev/null +++ b/scripts/start.js @@ -0,0 +1,40 @@ +#!/usr/bin/env node +const { execSync } = require("child_process") +const fs = require("fs") +const path = require("path") + +const root = path.resolve(__dirname, "..") +const standaloneDir = path.join(root, ".next", "standalone") +const staticSrc = path.join(root, ".next", "static") +const staticDst = path.join(standaloneDir, ".next", "static") + +if (!fs.existsSync(standaloneDir)) { + console.error("ERROR: .next/standalone/ not found. Run 'npm run build' first.") + process.exit(1) +} + +if (!fs.existsSync(staticDst)) { + console.log("Copying static files to standalone output...") + fs.cpSync(staticSrc, staticDst, { recursive: true }) +} + +const server = path.join(standaloneDir, "server.js") +if (!fs.existsSync(server)) { + console.error("ERROR: server.js not found in standalone output.") + process.exit(1) +} + +process.env.NODE_ENV = "production" + +const args = process.argv.slice(2) +const child = require("child_process").spawn("node", [server, ...args], { + cwd: standaloneDir, + stdio: "inherit", + env: { + ...process.env, + PORT: process.env.PORT || "3000", + HOSTNAME: process.env.HOSTNAME || "0.0.0.0", + }, +}) + +child.on("exit", (code) => process.exit(code))