可能将crossorigin属性添加到由角度cli生成的脚本标签中? [英] possible to add crossorigin attribute to script tags generated by angular cli?

查看:149
本文介绍了可能将crossorigin属性添加到由角度cli生成的脚本标签中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将crossorigin属性添加到由角度cli生成的脚本标签中?

Is it possible to add crossorigin attribute to script tags generated by angular cli?

在运行我的角度应用程序时,脚本标签会添加到index.html的末尾:

when running my angular app, script tags are added to the end of my index.html:

<脚本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>

是否可以配置angular-cli,以便在构建index.html中包含这些标签时,将crossorigin属性添加到脚本声明中:

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:

< script src ="runtime-es2015.3d05cbd29d24231258bf.js" type ="module" crossorigin ="use-credentials"></script>

我为什么要问这个问题??我的应用程序已部署到使用基本身份验证的apache服务器中.当使用Firefox或Edge(chrome很好)时,由于未设置Authorization标头,因此对javascript模块的请求会收到401错误.如果添加了跨域标签,则会设置授权标头.因此,如果我希望我的用户能够使用FF或Edge,则需要能够添加该跨域属性.

Why am I asking this? My application is deployed to an apache server that uses basic authentication. 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. If the crossorigin tag is added, the Authorization header is set. Therefore, I need to be able to add that crossorigin attribute if I want my users to be able to use FF or Edge.

感谢您阅读我的问题.

推荐答案

从angular/cli 8.1开始(

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

-crossOrigin = none |匿名|使用凭证
定义提供CORS支持的元素的crossorigin属性设置.默认值:无
https://angular.io/cli/build

另一种选择是使用构建后的npm步骤来解开index.html并根据各种需要对其进行更改

another alternative would have been to use a post build npm step that unwraps the index.html and alters it based on various needs

cheeriojs 会在服务器端进行此类操作(请注意示例了解更多详细信息)

cheeriojs comes in mind for these kind of manipulations on the server side (check this example for some more details)

这篇关于可能将crossorigin属性添加到由角度cli生成的脚本标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆