css 消除amp页面上的呈现阻止资源字体

7nbnzgx9  于 2023-03-14  发布在  其他
关注(0)|答案(2)|浏览(88)

Google Lighthouse引发AMP页面

问题
我尝试了多种解决方案,但均无效

<link rel="preload" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,600,700,900&display=swap" as="font" crossorigin>
<link rel="preload" href="https://fonts.googleapis.com/css?family=Cairo:300,400,600,700&subset=arabic,latin-ext&display=swap" as="font" crossorigin>
wqlqzqxt

wqlqzqxt1#

您可以添加一个onload侦听器,以将rel属性从preload更改为stylesheet
这将看起来像这样:

<link
  rel="preload"
  href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,600,700,900&display=swap"
  onload="this.onload=null;this.rel='stylesheet'"
  as="style"
>

有关此主题的详细信息,请访问web.dev

b4lqfgs4

b4lqfgs42#

web.dev表示您应该以这种方式预加载:

<link rel="preload" 
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,600,700,900&display=swap"
      as="style"
      onload="this.onload=null; this.rel='stylesheet'; document.body.classList.add('fonts-loaded')">

但在加载字体之前使用另一种字体:

body {
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

body.fonts-loaded {
  font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

相关问题