【问题标题】:IIS + compression-webpack-plugin (gzip) - Loading failed for the "script" with sourceIIS + compression-webpack-plugin (gzip) - 使用源代码加载“脚本”失败
【发布时间】:2019-05-02 15:22:09
【问题描述】:

我在我的 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>

Web 浏览器不加载构建脚本后 - 我对控制台中的每个脚本文件都有警告:

使用源代码加载“脚本”失败 “http://192.168.100.100:8088/Scripts/dist/runtime.7b9bc97b36a783fd7495.js”。

我做错了什么?我应该在后端设置其他东西吗?请注意,我包含带有 .js 扩展名的脚本,而不是 .js.gz - 这是一个错误吗?

【问题讨论】:

    标签: iis asp.net-mvc-5 compression gzip webpack-4


    【解决方案1】:

    好的,终于,经过深入搜索,我找到了解决方案。

    1. 在 IIS 中为项目禁用动态和静态压缩(因为我已经压缩了文件,所以不要理会 CPU!)

    1. 从这里下载并安装 IIS 的 URL 重写模块:https://www.iis.net/downloads/microsoft/url-rewrite

    2. 从 Web.config 下面的行中删除(如果仍然存在):

    &lt;urlCompression doStaticCompression="true" doDynamicCompression="true" /&gt;

    1. 在下面的代码中添加到 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>
    1. 确保您在 IIS 中的 MIME 类型中有 .gz:

    【讨论】:

    • 太棒了,谢谢。找了好几个小时了。
    • 如果我只有.gz 文件只用于.js.css 怎么办?我可以只配置这些类型吗?浏览器会不会因为不喜欢混合环境而报错?
    猜你喜欢
    • 2020-11-29
    • 2018-03-13
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2017-01-16
    • 2018-02-03
    • 2013-03-18
    • 2014-04-08
    相关资源
    最近更新 更多