Просмотр исходного кода

fix: avoid Livewire/Filament 404 from route cache built without production APP_KEY

Livewire v4 registers POST .../livewire-{hash}/update where hash is derived
from APP_KEY. Docker build excludes .env, so route:cache baked wrong URIs.
Remove route:cache from Dockerfile; clear routes on nginx container start;
clear routes in deploy.sh after cache clear.

Co-authored-by: Cursor <cursoragent@cursor.com>
yemeishu 3 дней назад
Родитель
Сommit
327140c511
3 измененных файлов с 8 добавлено и 5 удалено
  1. 4 5
      Dockerfile
  2. 2 0
      deploy.sh
  3. 2 0
      docker-entrypoint.sh

+ 4 - 5
Dockerfile

@@ -123,9 +123,9 @@ RUN apk add --no-cache nginx && \
 # 从第一阶段复制构建好的前端资源
 COPY --from=frontend-builder /app/public/build ./public/build
 
-# 缓存路由和视图(不缓存配置,配置在运行时从 .env 读取)
-RUN php artisan route:cache && \
-    php artisan view:cache && \
+# 不在镜像构建时 route:cache:.dockerignore 排除了 .env,构建时 APP_KEY 与生产不一致;
+# Livewire v4 的 /livewire-{hash}/update 前缀由 APP_KEY 派生,错缓存会导致 /admin 下 Livewire 404。
+RUN php artisan view:cache && \
     php artisan filament:upgrade || true
 
 # 复制 Nginx 和 PHP-FPM 配置
@@ -236,8 +236,7 @@ COPY --from=frontend-builder /app/public/build ./public/build
 RUN mkdir -p storage/logs storage/framework/cache storage/framework/sessions storage/framework/views bootstrap/cache && \
     chmod -R 775 storage bootstrap/cache
 
-RUN php artisan route:cache && \
-    php artisan view:cache && \
+RUN php artisan view:cache && \
     php artisan filament:upgrade || true
 
 COPY docker/nginx.conf /etc/nginx/nginx.conf

+ 2 - 0
deploy.sh

@@ -87,11 +87,13 @@ clear_cache() {
         local container=$(docker ps --format '{{.Names}}' | grep 'math_cms_app' | head -1)
         docker exec "$container" php artisan view:clear 2>/dev/null || true
         docker exec "$container" php artisan cache:clear 2>/dev/null || true
+        docker exec "$container" php artisan route:clear 2>/dev/null || true
         ok "通过容器清除缓存"
     else
         # 容器未运行时本地执行(需要 PHP)
         php artisan view:clear 2>/dev/null || true
         php artisan cache:clear 2>/dev/null || true
+        php artisan route:clear 2>/dev/null || true
         ok "本地清除缓存"
     fi
 }

+ 2 - 0
docker-entrypoint.sh

@@ -16,6 +16,8 @@ fi
 
 # 启动 PHP-FPM(后台运行)
 if [ "$1" = "nginx" ]; then
+    # 清除可能存在的 route 缓存(构建时无 .env 的 route:cache 会导致 Livewire 前缀与运行时不一致)
+    php artisan route:clear 2>/dev/null || true
     echo "Starting PHP-FPM..."
     php-fpm -D
     echo "PHP-FPM started"