reactjs - 由于 Reactjs 中的无效 MIME 类型问题,拒绝执行 JS 块
问题描述
问题声明:
Refused to execute script from 'https://something.test.in/21.13888aeefb423ea1abff.chunk.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
可能导致问题的软件包:
- 反应样板v3.4.0
- 反应v15.4.1
- Webpack v2.3.3
- 离线插件v4.5.2
问题是随机复制的,无法通过任何方法追踪
问题的可能原因: 部署后服务人员更新和安装
针对上述原因申请的解决方案:
if (process.env.NODE_ENV === 'production') {
OfflinePluginRuntime.install({
onUpdating: () => {
console.log('SW Event:', 'onUpdating');
},
onUpdateReady: () => {
console.log('SW Event:', 'onUpdateReady');
// Update is applied here
OfflinePluginRuntime.applyUpdate();
},
onUpdated: () => {
console.log('SW Event:', 'onUpdated');
// Force reload happens here
window.location.reload();
},
onUpdateFailed: () => {
console.log('SW Event:', 'onUpdateFailed');
},
});
}
结果:问题未解决
用于生产的 Webpack 配置:
// Important modules this config uses
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const OfflinePlugin = require('offline-plugin');
module.exports = require('./webpack.base.babel')({
// In production, we skip all hot-reloading stuff
entry: {
main: path.join(process.cwd(), 'app/app.js'),
},
// Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
output: {
filename: '[name].[chunkhash].js',
chunkFilename: '[name].[chunkhash].chunk.js',
},
devtool: false,
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: ({ resource }) => {
if (resource && (/^.*\.(css|scss)$/).test(resource)) {
return false;
}
return /node_modules/.test(resource);
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'bootstrap',
minChunks: Infinity,
}),
// Minify and optimize the index.html
new HtmlWebpackPlugin({
template: 'app/index.html',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
inject: true,
}),
// Put it in the end to capture all the HtmlWebpackPlugin's
// assets manipulations and do leak its manipulations to HtmlWebpackPlugin
new OfflinePlugin({
relativePaths: false,
publicPath: '/',
// No need to cache .htaccess. See http://mxs.is/googmp,
// this is applied before any match in `caches` section
excludes: ['.htaccess'],
caches: {
main: [':rest:'],
// All chunks marked as `additional`, loaded after main section
// and do not prevent SW to install. Change to `optional` if
// do not want them to be preloaded at all (cached only when first loaded)
additional: ['*.chunk.js'],
},
// Removes warning for about `additional` section usage
safeToUseOptionalCaches: true,
AppCache: false,
ServiceWorker: {
events: true,
},
}),
],
performance: {
assetFilter: (assetFilename) => !(/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename)),
},
});
解决方案
推荐阅读
- javascript - 在文件夹中的html中显示多个图像
- php - PHP paypal 商务平台(api v2)合作伙伴没有得到报酬?
- kubernetes - Kubespray Kubernetes 安装失败 - dockerd[8296]:无法使用文件 /etc/docker/daemon.json 配置 Docker 守护进程
- python-3.x - 无法通过 API Gateway 将对象解析为事件以获取 Lambda 的响应?
- python - 如何使用窗口函数计算 pyspark 中的日期差异?
- ios - 在网页中执行操作时没有网络时关闭 WKWebview
- python - Python DictWriter 使用循环和 if else 条件在输入文件的列中附加状态
- html - 试图通过抓取找到超链接
- android - Android 下载管理器“下载失败”
- class - 涉及模板化表单时的 UML 类图