控制台缩小时出错 "Refused to execute because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled"

Error on minification in console "Refused to execute because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled"

我需要缩小文件夹 'js' 中的所有 js 文件(使用 Adob​​e Experience Manager),该文件夹还包含 'angular.min.js'.

在尝试这样做时,它会在 chrome 中引发错误,因为

Refused to execute script 'angular.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

我该如何解决这个问题?

我建议您将 Angular 等第三方库与您自己的客户端库分开。因为你通常不想缩小那些第三方库,但你确实想缩小你自己的客户端库。

示例:

etc/
├── clientlibs/
|   └── myproject/
|       └── vendor/
|           └── angular/
|               ├── angular.min.js
|               ├── .content.xml
|               └── js.txt
└── designs/
    └── myproject/
        └── myclientlib/
            ├── js/
            |   └── myclientlib.js
            ├── .content.xml
            └── js.txt

那么您可以使用以下 clientlib 定义:

/etc/clientlibs/myproject/vendor/angular/.content.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
      jcr:primaryType="cq:ClientLibraryFolder"
      categories="[myproject.angular]"
      jsProcessor="[default:none]"/>

jsProcessor="[default:none]" 禁用缩小。

/etc/designs/myproject/myclientlib/.content.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
      jcr:primaryType="cq:ClientLibraryFolder"
      categories="[myproject.myclientlib]"
      jsProcessor="[min:gcc]"/>

jsProcessor="[min:gcc]" 启用 Google 闭包编译器的缩小。

您可以在 HTL(以前的 Sightly)模板中轻松使用这两个客户端库:

<sly data-sly-use.clientlib="/libs/granite/sightly/templates/clientlib.html">
    <sly data-sly-call="${clientlib.js @ categories='myproject.angular'}"/>
    <sly data-sly-call="${clientlib.js @ categories='myproject.myclientlib'}"/>
</sly>

注意:您可以自由选择clientlib类别名称myproject.angularmyproject.myclientlib。您不必在我的示例中使用这些名称。但我建议使用某种名称空间,例如 myproject.