拒绝执行脚本,因为它的MIME类型('text/html')不可执行,并且启用了严格的MIME类型检查

cho*_*bo2 11 webpack react-router webpack-4

我正在尝试设置反应路由,当我点击我的网站上的某些东西时路由有效,但是如果我打开一个新的选项卡并复制该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.
Run Code Online (Sandbox Code Playgroud)

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'
    },
  }
};
Run Code Online (Sandbox Code Playgroud)

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')
);
Run Code Online (Sandbox Code Playgroud)

商店

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

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

export default stores;
Run Code Online (Sandbox Code Playgroud)

我也试过了 historyApiFallback: true

Adn*_*hah 8

只需编辑以下内容即可编辑webpack配置:

    devServer: {
                 historyApiFallback: true
               }
Run Code Online (Sandbox Code Playgroud)

并将此添加到public/index.html:

     <base href="/" />
Run Code Online (Sandbox Code Playgroud)

这应该做的伎俩..

  • 如果你在webpack配置中添加它,则不需要带有`<base href ="/"/>`的部分:`output:{publicPath:'/'}` (2认同)

Omr*_*zon 6

您的webpack配置格式不正确.所以你的devServer返回了fallback html文件而不是bundle.

因此,为什么脚本与('text/html')MIME类型一起提供.

devServer: {
    historyApiFallback:{
      index:'/dist/index.html'
    },
  }
Run Code Online (Sandbox Code Playgroud)

你可能意味着这样的事情:

devServer: {
  historyApiFallback: true
}
Run Code Online (Sandbox Code Playgroud)

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