我正在尝试设置反应路由,当我单击我网站上的某些内容时,该路由有效,但是如果我打开一个新选项卡并复制该 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'
},
}
};
索引.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
只需通过编辑以下内容来编辑 webpack 配置:
devServer: {
historyApiFallback: true
}
并将其添加到 public/index.html:
<base href="/" />
这应该可以解决问题..
您的 webpack 配置格式不正确。因此,您的开发服务器将返回回退 html 文件而不是捆绑包。
因此,为什么脚本与("text/html"(MIME类型一起提供。
devServer: {
historyApiFallback:{
index:'/dist/index.html'
},
}
你可能的意思是这样的:
devServer: {
historyApiFallback: true
}
https://webpack.js.org/configuration/dev-server/
也许你指向不正确
<script src="utils.js"></script> // NOT ok. Refused to execute script..MIME
<script src="js/utils.js"></script> // OK
我最近不得不将以下内容添加到标头中,作为安全审计解决方案的一部分
X-Content-Type-Options: nosniff
在那之后,我开始收到这些错误。我还没有找到一个永久的解决方案。不过,这些页面似乎正在工作。控制台中的噪音很烦人!
我得到了同样的错误,发现我需要更改我的脚本
索引.html
从
<script src="App.js"></script>
自
<script src="/App.js"></script>
因此,如果您的文件前面有"."或没有任何内容,只需将其替换为"/"。我希望这对您或其他人有所帮助。
可能导致此问题的另一件事是,如果 contentBase
属性设置不正确。 这对我们来说是因为我们请求了一个 JS 文件,但得到了一个 404,webpack-dev-server 提供的 404"页面"作为text/html
返回。
就我而言,我遇到了这个问题,因为构建过程实际上并没有构建JavaScript捆绑包。
生成只是发出警告而不是错误 (!(,因此只有在将生成推送到分阶段部署环境时才会发现此问题。
因此,为了解决此问题,我修复了未构建捆绑包的问题。
好吧,如果有人来这里寻求可能的解决方案,我为我的情况找到了另一个解决方案。将--no-module
标志添加到 npm 脚本。我的问题出在生产构建上,所以在我的情况下,package.json
;
"build": "vue-cli-service build --no-module",