[英]possible to add crossorigin attribute to script tags generated by angular cli?
Is it possible to add crossorigin attribute to script tags generated by angular cli?是否可以将 crossorigin 属性添加到由 angular cli 生成的脚本标签?
when running my angular app, script tags are added to the end of my index.html:运行我的 angular 应用程序时,脚本标签被添加到我的 index.html 的末尾:
<script src="runtime-es2015.3d05cbd29d24231258bf.js" type="module"></script>
<script src="polyfills-es2015.28da6787754ec8436843.js" type="module"></script>
<script src="main-es2015.4106b7f4d43a05cb792d.js" type="module"></script>
Is it possible to configure angular-cli so that when those tags are included in the build index.html, that the crossorigin attribute be added to the script declaration:是否可以配置 angular-cli,以便在构建 index.html 中包含这些标签时,将 crossorigin 属性添加到脚本声明中:
<script src="runtime-es2015.3d05cbd29d24231258bf.js" type="module" crossorigin="use-credentials"></script>
Why am I asking this?我为什么要问这个? My application is deployed to an apache server that uses basic authentication.
我的应用程序部署到使用基本身份验证的 apache 服务器。 When using Firefox or Edge (chrome is fine) the requests for the javascript modules receive a 401 error because the Authorization header is not set.
使用 Firefox 或 Edge(chrome 很好)时,对 javascript 模块的请求会收到 401 错误,因为未设置 Authorization 标头。 If the crossorigin tag is added, the Authorization header is set.
如果添加了 crossorigin 标记,则设置 Authorization 标头。 Therefore, I need to be able to add that crossorigin attribute if I want my users to be able to use FF or Edge.
因此,如果我希望我的用户能够使用 FF 或 Edge,我需要能够添加该 crossorigin 属性。
Thank you for reading my question.感谢您阅读我的问题。
As of angular/cli 8.1 ( PR ) there is a flag that can be set to alter the script tags (and link tags for that matter) in the index.html从 angular/cli 8.1 ( PR ) 开始,可以设置一个标志来更改 index.html 中的脚本标签(以及与此相关的链接标签)
--crossOrigin=none|anonymous|use-credentials
--crossOrigin=none|匿名|使用凭证
Define the crossorigin attribute setting of elements that provide CORS support.定义提供 CORS 支持的元素的 crossorigin 属性设置。 Default: none https://angular.io/cli/build
默认值:无https://angular.io/cli/build
another alternative would have been to use a post build npm step that unwraps the index.html and alters it based on various needs另一种选择是使用 post build npm 步骤来解包 index.html 并根据各种需要对其进行更改
cheeriojs comes in mind for these kind of manipulations on the server side (check this example for some more details)对于服务器端的这些操作, cheeriojs想到了(查看此示例以获取更多详细信息)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.