IIS + compression-webpack-plugin (gzip) - "script" 的源加载失败
IIS + compression-webpack-plugin (gzip) - Loading failed for the "script" with source
我在 ASP.NET MVC 5 项目中使用 compression-webpack-plugin 将我的 javascript 文件压缩成 gz 格式。
我的 webpack.config.js 的一部分 compression-webpack-plugin 设置:
const CompressionPlugin = require('compression-webpack-plugin');
module.exports = {
//...
plugins: [
//...
new CompressionPlugin({
test: /\.(js|css)$/,
filename: '[path].gz[query]',
algorithm: 'gzip',
deleteOriginalAssets: true
}),
],
//...
};
它工作正常:
下一步是在 IIS 中启用 GZIP 压缩,所以首先我要确保我在 Windows 功能:
中有必要的功能
...并直接在 IIS 中为我的应用程序启用压缩,如下图所示。
此外,我已将这段代码添加到我的 Web.config:
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
构建脚本后网络浏览器未加载 - 我在控制台中收到每个脚本文件的警告:
Loading failed for the "script" with source
„http://192.168.100.100:8088/Scripts/dist/runtime.7b9bc97b36a783fd7495.js”.
我做错了什么?我应该在后端设置其他东西吗?请注意,我包含扩展名为 .js 的脚本,而不是 .js.gz - 这是一个错误?
好的,经过深入搜索,我终于找到了解决方案。
- 在 IIS 中为项目禁用动态和静态压缩(因为我已经对文件进行了 gzip 压缩,所以不要管 CPU!)
从此处下载并安装 URL IIS 重写模块:https://www.iis.net/downloads/microsoft/url-rewrite
从 Web.config 下面的行(如果仍然存在)中删除:
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
- 添加到Web.config下面的一段代码:
<system.webServer>
<staticContent>
<remove fileExtension=".js.gz" />
<remove fileExtension=".css.gz" />
<remove fileExtension=".png.gz" />
<remove fileExtension=".jpg.gz" />
<remove fileExtension=".gif.gz" />
<remove fileExtension=".svg.gz" />
<remove fileExtension=".html.gz" />
<remove fileExtension=".json.gz" />
<mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
<mimeMap fileExtension=".css.gz" mimeType="text/css" />
<mimeMap fileExtension=".png.gz" mimeType="image/png" />
<mimeMap fileExtension=".jpg.gz" mimeType="image/jpeg" />
<mimeMap fileExtension=".gif.gz" mimeType="image/gif" />
<mimeMap fileExtension=".svg.gz" mimeType="image/svg+xml" />
<mimeMap fileExtension=".html.gz" mimeType="text/html" />
<mimeMap fileExtension=".json.gz" mimeType="application/json" />
</staticContent>
<rewrite>
<outboundRules rewriteBeforeCache="true">
<rule name="Custom gzip file header">
<match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="\.gz$" />
</conditions>
<action type="Rewrite" value="gzip"/>
</rule>
</outboundRules>
<rules>
<rule name="Rewrite gzip file">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
<add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.gz" />
</rule>
</rules>
</rewrite>
</system.webServer>
- 确保您在 IIS 的 MIME 类型中有 .gz:
我在 ASP.NET MVC 5 项目中使用 compression-webpack-plugin 将我的 javascript 文件压缩成 gz 格式。
我的 webpack.config.js 的一部分 compression-webpack-plugin 设置:
const CompressionPlugin = require('compression-webpack-plugin');
module.exports = {
//...
plugins: [
//...
new CompressionPlugin({
test: /\.(js|css)$/,
filename: '[path].gz[query]',
algorithm: 'gzip',
deleteOriginalAssets: true
}),
],
//...
};
它工作正常:
下一步是在 IIS 中启用 GZIP 压缩,所以首先我要确保我在 Windows 功能:
中有必要的功能...并直接在 IIS 中为我的应用程序启用压缩,如下图所示。
此外,我已将这段代码添加到我的 Web.config:
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
构建脚本后网络浏览器未加载 - 我在控制台中收到每个脚本文件的警告:
Loading failed for the "script" with source „http://192.168.100.100:8088/Scripts/dist/runtime.7b9bc97b36a783fd7495.js”.
我做错了什么?我应该在后端设置其他东西吗?请注意,我包含扩展名为 .js 的脚本,而不是 .js.gz - 这是一个错误?
好的,经过深入搜索,我终于找到了解决方案。
- 在 IIS 中为项目禁用动态和静态压缩(因为我已经对文件进行了 gzip 压缩,所以不要管 CPU!)
从此处下载并安装 URL IIS 重写模块:https://www.iis.net/downloads/microsoft/url-rewrite
从 Web.config 下面的行(如果仍然存在)中删除:
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
- 添加到Web.config下面的一段代码:
<system.webServer>
<staticContent>
<remove fileExtension=".js.gz" />
<remove fileExtension=".css.gz" />
<remove fileExtension=".png.gz" />
<remove fileExtension=".jpg.gz" />
<remove fileExtension=".gif.gz" />
<remove fileExtension=".svg.gz" />
<remove fileExtension=".html.gz" />
<remove fileExtension=".json.gz" />
<mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
<mimeMap fileExtension=".css.gz" mimeType="text/css" />
<mimeMap fileExtension=".png.gz" mimeType="image/png" />
<mimeMap fileExtension=".jpg.gz" mimeType="image/jpeg" />
<mimeMap fileExtension=".gif.gz" mimeType="image/gif" />
<mimeMap fileExtension=".svg.gz" mimeType="image/svg+xml" />
<mimeMap fileExtension=".html.gz" mimeType="text/html" />
<mimeMap fileExtension=".json.gz" mimeType="application/json" />
</staticContent>
<rewrite>
<outboundRules rewriteBeforeCache="true">
<rule name="Custom gzip file header">
<match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="\.gz$" />
</conditions>
<action type="Rewrite" value="gzip"/>
</rule>
</outboundRules>
<rules>
<rule name="Rewrite gzip file">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
<add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.gz" />
</rule>
</rules>
</rewrite>
</system.webServer>
- 确保您在 IIS 的 MIME 类型中有 .gz: