我正在尝试设置react路由,当我在我的网站上点击一些东西时,路由就会工作,但是如果我打开一个新的标签并复制那个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')
);
商店
import {RouterStore} from 'mobx-react-router';
const routingStore = new RouterStore();
const stores = {
routingStore
}
export default stores;
我也尝试过historyApiFallback: true
发布于 2018-04-11 02:34:57
您的webpack配置格式不正确。因此,您的devServer将返回备用html文件,而不是包。
这就是为什么脚本使用('text/html') MIME类型。
devServer: {
historyApiFallback:{
index:'/dist/index.html'
},
}
你的意思可能是这样的:
devServer: {
historyApiFallback: true
}
发布于 2018-06-14 15:38:49
只需编辑以下内容即可编辑webpack配置:
devServer: {
historyApiFallback: true
}
并将其添加到public/index.html:
<base href="/" />
这个应该行得通..
发布于 2018-06-16 04:53:26
也许你是指错了
<script src="utils.js"></script> // NOT ok. Refused to execute script..MIME
<script src="js/utils.js"></script> // OK
https://stackoverflow.com/questions/49760641
复制相似问题