【问题标题】:PageSpeed Insights diagnostic "Ensure text remains visible during webfont load"PageSpeed Insights 诊断“确保文本在 webfont 加载期间保持可见”
【发布时间】:2020-11-03 11:01:31
【问题描述】:

我正在对PageSpeed Insights 进行诊断

Ensure text remains visible during webfont load
Leverage the font-display CSS feature to ensure text is user-visible while webfonts are loading. Learn more.
URL
    
Potential Savings
…v1/aWB4m0aac….woff2(fonts.gstatic.com)     10 ms
…v21/nuFvD-vYS….woff2(fonts.gstatic.com)    10 ms
…v13/6xK3dSBYK….woff2(fonts.gstatic.com)    10 ms

我正在尝试通过更改来纠正它

  <link rel="stylesheet" href="https://fonts.googleapis.com" />

<link rel="stylesheet" href="https://fonts.googleapis.com/display=swap" />

但这实际上会引发控制台错误


我知道PageSpeed Insights recommends to add font-display: swap@fontface,但我不知道@fontface 是什么。

我正在使用 BootstrapGatsby

我知道还有gatsby-plugin-web-font-loader。有没有办法使用它来避免这种诊断?


这些都是 font-family 出现在我的程序中的所有时间。我用的是scss,所以大部分都是变量

我只在我的程序中指定了一次字体,我认为 bootstrap 会在其余时间选择字体

blockquote > p {
   font-family: 'Montserrat', sans-serif;
}

更新,我正在使用这个插件

{
  resolve: `gatsby-plugin-google-fonts`,
  options: {
    fonts: [
      `Montserrat`,
      `Helvetica Neue`,
      `Helvetica`,
      `Arial`
      
    ],
    display: 'swap'
  }
},

【问题讨论】:

    标签: javascript reactjs gatsby pagespeed pagespeed-insights


    【解决方案1】:

    你犯了一个小错误。

    应该是

    &lt;link rel="stylesheet" href="https://fonts.googleapis.com/css?family=TheFontYouWantToUse&amp;display=swap /&gt;

    如果您按照示例中所示执行正斜杠,则会导致 404 未找到,因此会出现控制台错误。将其替换为 URL 参数 (&amp;),它应该可以正常工作。

    @fontface 只是从样式表中加载字体的一种方式。

    例如,在您的主 CSS 文件中,您可以添加以下内容,这也会加载字体。注意 font-display 属性设置为 swap

    @font-face {
      font-family: 'Pacifico';
      font-style: normal;
      font-weight: 400;
      src: local('Pacifico Regular'), local('Pacifico-Regular'), url(https://fonts.gstatic.com/s/pacifico/v12/FwZY7-Qmy14u9lezJ-6H6MmBp0u-.woff2) format('woff2');
      font-display: swap;
    }
    

    【讨论】:

    • 我想使用的字体是我的css中提到的任何字体吗?我正在使用引导程序,所以他们没有指定一些字体吗?
    • 我在我的答案中添加了另一种在样式表中包含字体的方法。如果没有通过&lt;link&gt; 包含字体,可能会以这种方式包含字体,因此请查看您的样式表,看看是否可以找到@font-face
    • 请注意&lt;link rel="stylesheet" href="https://fonts.googleapis.com" /&gt; 自己不会做任何事情,所以请仔细检查,您必须使用family= 参数指定字体。这实际上可能是您的主题的问题。
    【解决方案2】:

    @font-face 是一个允许您使用多个font-family 规则而不是在每个选择器中加载它的规则。

    在 Gatsby 的所有字体插件中,我推荐 gatsby-plugin-google-fonts,因为它允许您在字体之间显示和交换。

     plugins: [
        {
          resolve: `gatsby-plugin-google-fonts`,
          options: {
            fonts: [
              `limelight`,
              `source sans pro\:300,400,400i,700` // you can also specify font weights and styles
            ],
            display: 'swap'
          }
        }
      ]
    

    它非常有用,因为它可以在不影响显示的情况下预加载字体(由于 swap 属性)。

    使用 Gatsby,&lt;link rel="stylesheet" href="https://fonts.googleapis.com" /&gt; 此配置是自动化的,因此您无需触摸它。最好使用插件预渲染它们,因为这是 Gatsby 的强大功能。

    【讨论】:

    • 我怎么知道要在其中写入哪些字体?我有一种用于 blockquotes(Montserrat) 的字体,然后我认为 bootstrap 会选择其余字体
    • 然后你应该在那里加载 Montserrat 和 Bootstrap 字体。由于包装的重量,引导程序以非常糟糕的方式影响了性能。使用自定义字体它会正确加载,我不知道它是否适用于 Bootstrap 字体...
    • 你知道我怎样才能知道引导字体是什么吗?
    • 检查您的网页上的 HTML 输出或查看您的网络。您应该能够查看是否有额外的 CSS 字体请求。
    • 谢谢,我有一个与这篇文章无关的问题,我正在尝试添加一个插件来加载recaptcha,但我只需要在我的两个网页上使用它。您知道如何指定仅将其包含在这些页面上吗?否则我认为 PageSpeed 认为它是未使用的 javascript。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2022-07-12
    相关资源
    最近更新 更多