【问题标题】:Laravel Vue production instanceLaravel Vue 生产实例
【发布时间】:2021-05-31 03:29:01
【问题描述】:

我有一个在开发中运行良好的 Laravel Vue 项目。几个小时以来,我一直在尝试将它发送到我们的生产服务器。但是,当我访问 URL 时,我得到一个空白页面。在控制台中查看,我注意到所有的 js 块文件返回状态为 200,但它们的内容是“我们很抱歉,但是如果没有 Javascript,项目名称将无法正常工作。”

以下是我的 webpack.mix.js 的 sn-p。

mix
  .js('resources/js/app.js', 'public/js')
  .webpackConfig({
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'frontend/src/'),
        '@themeConfig': path.resolve(__dirname, 'frontend/themeConfig.js'),
        '@core': path.resolve(__dirname, 'frontend/src/@core'),
        '@validations': path.resolve(__dirname, 'frontend/src/@core/utils/validations/validations.js'),
        '@axios': path.resolve(__dirname, 'frontend/src/libs/axios')
      }
    },
    module: {
      rules: [
        {
          test: /\.s[ac]ss$/i,
          use: [
            {
              loader: 'sass-loader',
              options: {
                sassOptions: {
                  includePaths: ['frontend/node_modules', 'frontend/src/assets']
                }
              }
            }
          ]
        },
        {
          test: /(\.(png|jpe?g|gif|webp)$|^((?!font).)*\.svg$)/,
          loaders: {
            loader: 'file-loader',
            options: {
              name: 'images/[path][name].[ext]',
              context: '../vuexy-vuejs-bootstrap-vue-template/src/assets/images'
              //   context: 'frontend/src/assets/images'
            }
          }
        }
      ]
    }
  })
  .sass('resources/sass/app.scss', 'public/css')
  .options({
    postCss: [require('autoprefixer'), require('postcss-rtl')]
  })

// ------------------------------------------------
// If you are deploying on subdomain/subfolder. Uncomment the below code before running the 'yarn prod' or 'npm run production' command.
// Please Change below 'publicPath' and 'setResourceRoot' options as per your sub-directory path. 
// ------------------------------------------------


 if (mix.inProduction()) {
   mix.version()
   mix.webpackConfig({
     output: {
       publicPath: '/app/',
       chunkFilename: 'js/chunks/[name].[chunkhash].js'
     }
   })
   mix.setResourceRoot('/app/')
 }else{ 
    mix.webpackConfig({
        output: {
            chunkFilename: 'js/chunks/[name].js'
        } 
    })
}

我在本地机器上使用 ff 设置生产:

npm run prod

然后我在服务器上通过 ssh 运行以下命令:

composer install --optimize-autoloader --no-dev

php artisan key:generate

在控制台中,js 块文件将其显示为来自服务器的响应:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="csrf-token" content="j9kLqaAInZ4043rRqT9Zn3kNMHaSfCdF7h5ejnnZ">
<link rel="icon" href="/favicon.ico">
<title>project Title</title>

<link rel="stylesheet" href="https://app.myprojects.com/css/app.css?id=bff15ac59733e85aedce">

<link rel="shortcut icon" href="https://app.myprojects.com/images/logo/favicon.png">
</head>
<body>
<noscript>
    <strong>We're sorry but this platform doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
  </noscript>
<div id="app">
</div>
<script src="https://app.myprojects.com/js/app.js?id=a74eb61fbd09ea2e1e92"></script>
</body>
</html>

任何帮助将不胜感激!

【问题讨论】:

    标签: laravel vue.js server shared-hosting


    【解决方案1】:

    经过大量进一步的调查,我注意到 app.css 和 app.js 都可以正确加载,并且只有块文件存在问题。显然他们是从 https://app.myproject.com/app/js/chunks/1.edcb1b13fece690b424f.js

    而不是来自

    https://app.myproject.com/js/chunks/1.edcb1b13fece690b424f.js

    我能够通过将 publicPath 和 resourceRoot 设置为 / 来正确加载页面

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 2023-03-05
      • 2020-04-03
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2018-05-09
      • 2020-07-20
      相关资源
      最近更新 更多