【问题标题】:Refused to load the stylesheet (meta tags dont work)拒绝加载样式表(元标记不起作用)
【发布时间】:2021-08-19 16:32:15
【问题描述】:

在我网站的已部署版本上,我看不到任何图标(来自材料设计图标),这是通过使用这样的样式表获得的

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.css">

我得到了错误

Refused to load the stylesheet 'https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.css' because it violates the following Content Security Policy directive: "style-src 'self' https://fonts.googleapis.com/ 'unsafe-inline'". Note that 'style-src-elem' was not explicitly set, so 'style-src' is used as a fallback.

Error that occured

我尝试过使用元标记,但失败了

<meta http-equiv="Content-Security-Policy" content="style-src 'self' https://fonts.googleapis.com/ 'unsafe-inline';">

如果有人知道如何解决这个错误,我将永远感激不尽

【问题讨论】:

标签: html css vue.js frontend material-design


【解决方案1】:

您的 CSP 似乎在 style-src 部分中不包含 https://cdn.jsdelivr.net。您更新后的 CSP 可能如下所示:

<meta http-equiv="Content-Security-Policy" content="style-src 'self' https://fonts.googleapis.com https://cdn.jsdelivr.net 'unsafe-inline';">

附带说明:使用值'unsafe-inline' 应该,顾名思义,只有在您知道自己在做什么的情况下才能使用。可以说内联 JS 比内联 CSS“更危险”,但仍然 - 如果您不需要这个值,那么只需将其删除。请参阅CSP style-src: 'unsafe-inline' - is it worth it? 当然,如果您使用的是 Angular 之类的框架,则可能需要启用此功能。但是,恕我直言,记住各个标签的含义/含义很重要。

【讨论】:

    【解决方案2】:

    另一种解决方案是在样式表或标签中使用导入:

        @import url('https://YourFontLink');
    

    如果这不起作用,你的链接有问题,CSS的roboto字体链接是here,只需选择@import单选按钮

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      相关资源
      最近更新 更多