You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目Vite生产构建后仍引用开发服务器端口问题

解决Laravel + Livewire中@vite指令不加载编译后资源的问题

以下是针对你的问题的具体排查和解决步骤:

  • 修正APP_ENV变量:Laravel默认识别的生产环境标识是production,而非prod。将.env中的APP_ENV=prod改为APP_ENV=production,然后重启Sail容器使配置生效:

    ./vendor/bin/sail down && ./vendor/bin/sail up -d
    
  • 清除全量缓存:仅清除视图缓存不够,需要清除配置和路由缓存,确保环境变量变更被读取:

    ./vendor/bin/sail artisan config:clear
    ./vendor/bin/sail artisan route:clear
    ./vendor/bin/sail artisan view:clear
    
  • 验证Vite构建清单:检查public/build目录下是否存在manifest.json文件,这是Vite生成的资源映射清单,@vite指令依赖它加载编译后的资源。如果该文件缺失,重新执行构建命令并查看是否有报错:

    ./vendor/bin/sail npm run build
    
  • 检查vite.config.js配置:确保配置文件正确集成Laravel和Livewire插件,示例配置如下:

    import { defineConfig } from 'vite';
    import laravel from 'laravel-vite-plugin';
    import livewire from '@livewire/vite-plugin';
    
    export default defineConfig({
        plugins: [
            laravel({
                input: ['resources/css/app.css', 'resources/js/app.js'],
                refresh: true,
            }),
            livewire(),
        ],
    });
    

    若未安装@livewire/vite-plugin,先执行安装命令:

    ./vendor/bin/sail npm install @livewire/vite-plugin --save-dev
    
  • 确认Blade中@vite指令用法:确保指令引用的是源码目录下的资源,而非硬编码的开发服务器路径,示例正确用法:

    @vite(['resources/css/app.css', 'resources/js/app.js'])
    

内容的提问来源于stack exchange,提问作者newtovaux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:52:10