gpt4 book ai didi

IIS + compression-webpack-plugin (gzip) - 使用源代码加载 "script"失败

转载 作者:行者123 更新时间:2023-12-01 01:43:41 30 4
gpt4 key购买 nike

我用 压缩 webpack 插件 用于在我的 ASP.NET MVC 5 项目中将我的 javascript 文件压缩为 gz 格式。

我的一部分 webpack.config.js 压缩 webpack 插件 设置:

const CompressionPlugin = require('compression-webpack-plugin');

module.exports = {
//...
plugins: [
//...
new CompressionPlugin({
test: /\.(js|css)$/,
filename: '[path].gz[query]',
algorithm: 'gzip',
deleteOriginalAssets: true
}),
],
//...
};

它工作正常:

enter image description here

下一步是在 IIS 中启用 GZIP 压缩,所以首先我确保我在 Windows 功能中具有必要的功能:

enter image description here

...并直接在 IIS 中为我​​的应用程序启用压缩,如下图所示。

enter image description here

另外,我已将这段代码添加到我的 Web.config :
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>

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

Loading failed for the "script" with source „http://192.168.100.100:8088/Scripts/dist/runtime.7b9bc97b36a783fd7495.js”.



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

最佳答案

好的,最后,经过真正的深入搜索,我找到了解决方案。

  • 在 IIS 中为项目禁用动态和静态压缩(因为我已经压缩了文件,所以别管 CPU!)

  • enter image description here
  • 从这里下载并安装 IIS 的 URL 重写模块: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:

  • enter image description here

    关于IIS + compression-webpack-plugin (gzip) - 使用源代码加载 "script"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558063/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com