Phoenix 1.4 + VueJS - delivering default HTML instead of JS files

I am trying to render Vue.js directly in my Phoenix 1.4 application. But the following error is occurring after installing Vue-router:

Refused to execute script from ‘http://localhost:4000/0.app.js
because its MIME type (‘text/html’) is not executable, and strict MIME
type checking is enabled.
[vue-router] Failed to resolve async component default: Error: Loading chunk 0 failed.
(error: http://localhost:4000/0.app.js)
Error: Loading chunk 0 failed.
(error: http://localhost:4000/0.app.js)
at HTMLScriptElement.onScriptComplete (app.js:109)

My Phoenix router.ex file looks as follows:

scope "/", Web do
  pipe_through :browser
  get "/*path", PageController, :card_index:
end

My webpack.config.js:

const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = (env, options) => ({
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  entry: {
    './js/app.js': glob.sync('./vendor/**/*.js').concat(['./js/app.js'])
  },
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '../priv/static/js')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.s(c|a)ss$/,
        use: [
          'css-loader',
          {
            loader: 'sass-loader',
            // Requires sass-loader@^7.0.0
            options: {
              implementation: require('sass'),
              fiber: require('fibers'),
              indentedSyntax: true // optional
            }
          }
        ]
      },
      { test: /\.(png|woff|woff2|eot|ttf|svg)$/, use: 'url-loader' }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({ filename: '../css/app.css' }),
    new CopyWebpackPlugin([{ from: 'static/', to: '../' }]),
    new VueLoaderPlugin()
  ]
});

I can see in the Chrome Dev Tools Network tab that the response from this request http://localhost:4000/0.app.js is the app.html.eex file.

enter image description here

<!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"/>
    <title>Phoenix Framework</title>
  </head>
  <body>
    <div id="vue-app"></div>
    <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
  </body>
</html>

Thanks for any help.

0.app.js is not served through Plug.Static when using the default options in phoenix. There’s a whitelist of files served at the root, which 0.app.js is not part of. The 200 status is probably the index.html you’re serving with the wildcard route.

2 Likes

I resolved by specifying publicPath in the webpack.config.js file:

output: {
  filename: 'app.js',
  path: path.resolve(__dirname, '../priv/static/js'),
  publicPath: "./js/" #here
},
1 Like