Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I get

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

like image 808
chobo2 Avatar asked Apr 10 '18 18:04

chobo2


People also ask

How do you fix because its MIME type (' text html ') is not a supported stylesheet MIME type and strict mime checking is enabled?

To Solve MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled ErrorJust make Sure Your File name and the name You are Using in Link Tag Both Are Same. For Example my File name is style. css Then My Link tag is Something like this:<link rel=”stylesheet” href=”style.


Video Answer


2 Answers

Just edit webpack config by editing this:

    devServer: {
                 historyApiFallback: true
               }

And also add this to public/index.html:

     <base href="/" />

This should do the trick..

like image 61
Adnan Avatar answered Oct 18 '22 03:10

Adnan


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/

like image 7
Omri Luzon Avatar answered Oct 18 '22 02:10

Omri Luzon