From c43bed396eb6d2b9e94adb0bca50894b9da655fd Mon Sep 17 00:00:00 2001 From: kilyabin <65072190+kilyabin@users.noreply.github.com> Date: Thu, 5 Mar 2026 14:21:42 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20postbuild.js=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BF=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - scripts/postbuild.js: новый скрипт для копирования public и .next/static в .next/standalone - package.json: запуск postbuild.js после npm run build - scripts/install.sh: использование postbuild.js вместо ручного копирования - scripts/manage.sh: использование postbuild.js вместо ручного копирования - next.config.js: убран outputFileTracingRoot (не работает в Next.js 16) Теперь стили и статические файлы должны корректно работать в продакшене --- next.config.js | 2 - package.json | 2 +- scripts/install.sh | 27 ++++---------- scripts/manage.sh | 32 +++++----------- scripts/postbuild.js | 88 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 45 deletions(-) create mode 100644 scripts/postbuild.js diff --git a/next.config.js b/next.config.js index a019c87..a25aa24 100644 --- a/next.config.js +++ b/next.config.js @@ -4,7 +4,6 @@ const nextConfig = { output: 'standalone', generateEtags: false, allowedDevOrigins: ['192.168.1.10'], - // Не используем outputFileTracingRoot для корректной работы standalone режима webpack: (config, { isServer }) => { // Исключаем fs и path из клиентской сборки if (!isServer) { @@ -16,7 +15,6 @@ const nextConfig = { } return config }, - // Указываем корневую директорию для устранения предупреждения о множественных lockfiles turbopack: { root: __dirname, }, diff --git a/package.json b/package.json index 93bd73d..54052b4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "dev": "node --disable-warning=DEP0169 ./node_modules/next/dist/bin/next dev --webpack -H 0.0.0.0", - "build": "next build --webpack", + "build": "next build --webpack && node scripts/postbuild.js", "start": "next start -H 0.0.0.0", "lint": "next lint" }, diff --git a/scripts/install.sh b/scripts/install.sh index 5e98b68..9e79e3f 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -236,26 +236,15 @@ fi echo -e "${YELLOW}Building the application...${NC}" npm run build -# Ensure public directory and static files are accessible in standalone build -echo -e "${YELLOW}Setting up static files...${NC}" -if [ -d "$INSTALL_DIR/.next/standalone" ]; then - # Copy public directory to standalone (always update) - if [ -d "$INSTALL_DIR/public" ]; then - echo -e "${YELLOW}Copying public directory to standalone...${NC}" - rm -rf "$INSTALL_DIR/.next/standalone/public" 2>/dev/null || true - cp -r "$INSTALL_DIR/public" "$INSTALL_DIR/.next/standalone/public" 2>/dev/null || true - fi - # Copy .next/static to standalone/.next/static (always update) - if [ -d "$INSTALL_DIR/.next/static" ]; then - echo -e "${YELLOW}Copying .next/static to standalone...${NC}" - rm -rf "$INSTALL_DIR/.next/standalone/.next/static" 2>/dev/null || true - mkdir -p "$INSTALL_DIR/.next/standalone/.next" - cp -r "$INSTALL_DIR/.next/static" "$INSTALL_DIR/.next/standalone/.next/static" 2>/dev/null || true - else - echo -e "${RED}Warning: .next/static directory not found!${NC}" - fi +# Post-build script already copies public and .next/static to standalone directory +# Just verify the files are in place +echo -e "${YELLOW}Verifying standalone build...${NC}" +if [ -d "$INSTALL_DIR/.next/standalone/public" ] && [ -d "$INSTALL_DIR/.next/standalone/.next/static" ]; then + echo -e "${GREEN}✓ Static files are in place${NC}" else - echo -e "${RED}Warning: .next/standalone directory not found!${NC}" + echo -e "${RED}Warning: Static files may be missing from standalone directory${NC}" + echo -e "${YELLOW}Running post-build script manually...${NC}" + node scripts/postbuild.js || true fi # Check if service user exists, create if not diff --git a/scripts/manage.sh b/scripts/manage.sh index 2325b11..919d7e6 100755 --- a/scripts/manage.sh +++ b/scripts/manage.sh @@ -191,30 +191,16 @@ case "$1" in echo -e "${RED}The .next/standalone directory will not be created if the build fails.${NC}" exit 1 fi - - # Ensure public directory and static files are accessible in standalone build - echo -e "${YELLOW}Setting up static files...${NC}" - if [ -d "$INSTALL_DIR/.next/standalone" ]; then - # Copy public directory to standalone (always update) - if [ -d "$INSTALL_DIR/public" ]; then - echo -e "${YELLOW}Copying public directory to standalone...${NC}" - rm -rf "$INSTALL_DIR/.next/standalone/public" 2>/dev/null || true - cp -r "$INSTALL_DIR/public" "$INSTALL_DIR/.next/standalone/public" 2>/dev/null || true - fi - # Copy .next/static to standalone/.next/static (always update) - if [ -d "$INSTALL_DIR/.next/static" ]; then - echo -e "${YELLOW}Copying .next/static to standalone...${NC}" - rm -rf "$INSTALL_DIR/.next/standalone/.next/static" 2>/dev/null || true - mkdir -p "$INSTALL_DIR/.next/standalone/.next" - cp -r "$INSTALL_DIR/.next/static" "$INSTALL_DIR/.next/standalone/.next/static" 2>/dev/null || true - else - echo -e "${RED}Warning: .next/static directory not found!${NC}" - fi + + # Post-build script already copies public and .next/static to standalone directory + # Just verify the files are in place + echo -e "${YELLOW}Verifying standalone build...${NC}" + if [ -d "$INSTALL_DIR/.next/standalone/public" ] && [ -d "$INSTALL_DIR/.next/standalone/.next/static" ]; then + echo -e "${GREEN}✓ Static files are in place${NC}" else - echo -e "${RED}Error: .next/standalone directory not found!${NC}" - echo -e "${RED}This usually means the build failed or output: 'standalone' is not configured correctly.${NC}" - echo -e "${RED}Please check next.config.js and ensure the build completed successfully.${NC}" - exit 1 + echo -e "${RED}Warning: Static files may be missing from standalone directory${NC}" + echo -e "${YELLOW}Running post-build script manually...${NC}" + node scripts/postbuild.js || true fi # Set ownership diff --git a/scripts/postbuild.js b/scripts/postbuild.js new file mode 100644 index 0000000..0b26cc2 --- /dev/null +++ b/scripts/postbuild.js @@ -0,0 +1,88 @@ +#!/usr/bin/env node + +/** + * Post-build script for Next.js standalone mode + * Copies public and .next/static files to .next/standalone directory + */ + +const fs = require('fs'); +const path = require('path'); + +const ROOT_DIR = process.cwd(); +const STANDALONE_DIR = path.join(ROOT_DIR, '.next', 'standalone'); +const PUBLIC_DIR = path.join(ROOT_DIR, 'public'); +const NEXT_STATIC_DIR = path.join(ROOT_DIR, '.next', 'static'); +const STANDALONE_PUBLIC_DIR = path.join(STANDALONE_DIR, 'public'); +const STANDALONE_NEXT_STATIC_DIR = path.join(STANDALONE_DIR, '.next', 'static'); + +// Helper function to copy directory recursively +function copyDir(src, dest) { + if (!fs.existsSync(src)) { + console.error(`Source directory does not exist: ${src}`); + return false; + } + + if (!fs.existsSync(dest)) { + fs.mkdirSync(dest, { recursive: true }); + } + + const entries = fs.readdirSync(src, { withFileTypes: true }); + + for (const entry of entries) { + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + + if (entry.isDirectory()) { + copyDir(srcPath, destPath); + } else { + fs.copyFileSync(srcPath, destPath); + } + } + + return true; +} + +// Helper function to remove directory recursively +function removeDir(dir) { + if (fs.existsSync(dir)) { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + +console.log('Post-build: Setting up standalone directory...'); + +// Check if standalone directory exists +if (!fs.existsSync(STANDALONE_DIR)) { + console.error('Error: .next/standalone directory not found!'); + console.error('Make sure output: "standalone" is set in next.config.js'); + process.exit(1); +} + +// Copy public directory +if (fs.existsSync(PUBLIC_DIR)) { + console.log(`Copying ${PUBLIC_DIR} to ${STANDALONE_PUBLIC_DIR}...`); + removeDir(STANDALONE_PUBLIC_DIR); + if (copyDir(PUBLIC_DIR, STANDALONE_PUBLIC_DIR)) { + console.log('✓ Public directory copied'); + } else { + console.error('✗ Failed to copy public directory'); + } +} else { + console.warn('Warning: public directory not found'); +} + +// Copy .next/static directory +if (fs.existsSync(NEXT_STATIC_DIR)) { + console.log(`Copying ${NEXT_STATIC_DIR} to ${STANDALONE_NEXT_STATIC_DIR}...`); + removeDir(STANDALONE_NEXT_STATIC_DIR); + if (copyDir(NEXT_STATIC_DIR, STANDALONE_NEXT_STATIC_DIR)) { + console.log('✓ .next/static directory copied'); + } else { + console.error('✗ Failed to copy .next/static directory'); + } +} else { + console.error('Error: .next/static directory not found!'); + process.exit(1); +} + +console.log('Post-build: Done!');