Refused to execute script from because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled
Asked Answered
H

8

31

Refused to execute script from because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled

I am trying to setup react routing which works when I click on something on my site the route works. However if I open a new tab and copy that URL.

Refused to execute script from 'http://localhost:8080/something/index_bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

webpack.config

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
        path: path.join(__dirname, "/dist"),
    filename: "index_bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.s?css$/,
        use: [
          {
            loader: "style-loader" // creates style nodes from JS strings
          },
          {
            loader: "css-loader" // translates CSS into CommonJS
          },
          {
            loader: "sass-loader" // compiles Sass to CSS
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html"
    })
  ],
  devServer: {
    historyApiFallback:{
      index:'/dist/index.html'
    },
  }
};

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider  } from 'mobx-react';
import { useStrict } from 'mobx';
import createBrowserHistory from 'history/createBrowserHistory';
import {syncHistoryWithStore } from 'mobx-react-router';
import { Router } from 'react-router'

import AppContainer from './components/App';

const browserHistory = createBrowserHistory();

import stores from '../src/stores/Stores';

const history = syncHistoryWithStore(browserHistory, stores.routingStore);

ReactDOM.render(
    <Provider {... stores}>
        <Router history={history}>
           <AppContainer />
        </Router>
    </Provider>,      
       document.getElementById('app')
);

stores

import {RouterStore} from 'mobx-react-router';

const routingStore = new RouterStore();
const stores = {
    routingStore
}

export default stores;

I also tried historyApiFallback: true

Holograph answered 10/4, 2018 at 18:27 Comment(0)
D
8

Your webpack config is malformed. So your devServer is returning the fallback html file instead of the bundle.

Hence why the script is served with the ('text/html') MIME type.

devServer: {
    historyApiFallback:{
      index:'/dist/index.html'
    },
  }

You probably meant something like this:

devServer: {
  historyApiFallback: true
}

https://webpack.js.org/configuration/dev-server/

Dunlap answered 10/4, 2018 at 18:34 Comment(3)
I'm having a similar issue but its not resolved by the use of historyApiFallback. This issue looks like it refers to Webpack 3 though, not Webpack 4.Mccormac
@Mccormac If you are using webpack-dev-server, it's most likely because of historyApiFallback serving you the html file instead of a script. I can't help you more than that without more details.Dunlap
#45133842 There is some magic html tag that corrected this for me shrugMccormac
H
23

Just edit webpack.config.js, editing this:

module.exports = {
  // ...
  devServer: {
    historyApiFallback: true
  }
}

And also add this to public/index.html:

<base href="/" />

This should do the trick..

Haswell answered 14/6, 2018 at 7:38 Comment(4)
The part with <base href="/" /> is not necessary if you add this in webpack config: output: { publicPath: '/' }Shantung
Added both - then it was fixed.Ornithomancy
I my case i needed both to fix my problem.Haswell
years without using stack overflow, logged here just to say thank for this answer.Baud
D
8

Your webpack config is malformed. So your devServer is returning the fallback html file instead of the bundle.

Hence why the script is served with the ('text/html') MIME type.

devServer: {
    historyApiFallback:{
      index:'/dist/index.html'
    },
  }

You probably meant something like this:

devServer: {
  historyApiFallback: true
}

https://webpack.js.org/configuration/dev-server/

Dunlap answered 10/4, 2018 at 18:34 Comment(3)
I'm having a similar issue but its not resolved by the use of historyApiFallback. This issue looks like it refers to Webpack 3 though, not Webpack 4.Mccormac
@Mccormac If you are using webpack-dev-server, it's most likely because of historyApiFallback serving you the html file instead of a script. I can't help you more than that without more details.Dunlap
#45133842 There is some magic html tag that corrected this for me shrugMccormac
V
5

Maybe you are pointing incorrectly

<script src="utils.js"></script>        // NOT ok. Refused to execute script..MIME

<script src="js/utils.js"></script>    // OK
Verrocchio answered 15/6, 2018 at 20:53 Comment(0)
S
3

I recently had to add the following to the headers as part of a security audit resolution

X-Content-Type-Options: nosniff

After that i started getting these errors. I am yet to figure out a permanent solution for this. The pages seems to be working though. The noise in the console is just annoying!

Schizophyceous answered 8/5, 2018 at 20:5 Comment(0)
C
1

I got the same exact error and find out that I need to change my script in

index.html

from

  <script src="App.js"></script>

to

  <script src="/App.js"></script>

so if you have a "." or nothing in front of your file, just replace it with "/". I hope this helps you or someone else.

Coriolanus answered 11/10, 2018 at 0:13 Comment(0)
V
0

Another thing that can cause this is if the contentBase property is not set correctly. This came up for us in that we were requesting a JS file but getting a 404 and the 404 "page" served by webpack-dev-server comes back as text/html.

Valdovinos answered 28/8, 2018 at 18:4 Comment(0)
L
0

In my case, I encountered this issue because the build process did not actually build the JavaScript bundle.

The build just emitted a warning instead of an error(!), and so this issue was only found when the build was pushed to a staged deployment environment.

Hence to fix this issue I fixed the problem with the bundle not being built.

Lise answered 24/4, 2019 at 14:25 Comment(0)
P
0

Okay if someone came here for a possible solution, I found another one for my case. Add --no-module flag to your npm script. My problem was on production build, so in my case under the package.json;

"build": "vue-cli-service build --no-module",
Proverbial answered 12/4, 2022 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.