简体   繁体   English

可以将 crossorigin 属性添加到由 angular cli 生成的脚本标签吗?

[英]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.

相关问题 使用 require js 向脚本标签添加 crossorigin 属性 - add crossorigin attribute to script tag with require js 通过 JavaScript 向动态生成的脚本标签添加 defer 或 async 属性 - Add defer or async attribute to dynamically generated script tags via JavaScript 如何配置 vue 应用程序(使用 vue-cli)以将 nonce 属性添加到生成的脚本标签中? - How to configure a vue application (with vue-cli) to add nonce attributes to generated script tags? 如何将交叉源添加到<script> tag? - How to add just crossorigin to <script> tag? 如何将 js 中的 crossorigin='anonymous' 添加到脚本中 - how to add crossorigin='anonymous' from js to script 在图片加载前给所有 img 标签添加 crossorigin=&quot;anonymous&quot; - Add crossorigin="anonymous" to all img tags before the images are loaded 如何将“crossorigin”标签添加到动态加载的脚本中? - How do I add the “crossorigin” tag to a dynamically loaded script? 如何将自定义代码添加到 Angular cli 生成的 Service Worker - How to add custom code to Angular cli generated service worker 在脚本标签中反应跨域 - React crossorigin in Script tag 有没有办法在 Nuxt 中为缩小的 JS 脚本标签添加属性? - Is there any way to add an attribute to a minified JS script tags in Nuxt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM