49

Light House audit is suggesting that I preload key requests, specifically the two google fonts that I'm using in my React app. A Light House member suggested using: <link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto:700" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>

I know it's making the request because I see it in the waterfall and I get this console warning:

"The resource https://fonts.googleapis.com/css?family=Open+Sans|Roboto:700 was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate as value and it is preloaded intentionally."

Unfortunately the two font do not display in my app anymore. Do I need to define these in my CSS with @font-face or something like that?

wildair
  • 577
  • 2
  • 5
  • 12
  • I can't see your code, but I'd guess you *replaced* the `` with the ``. This is not sufficient. You'll need both, the `preload` and the `stylesheet`. – Loilo Jun 12 '18 at 19:37
  • 1
    So like this? – wildair Jun 12 '18 at 20:51
  • Not quite yet. You're not supposed to combine those tags into one but rather preserve both. You'll eventually end up with two `` tags for each font. One for `preload`, one `stylesheet`. – Loilo Jun 12 '18 at 20:53
  • This works: – wildair Jun 12 '18 at 21:26
  • Without adding the crossorgin property I get a render blocking stylesheet warning from LightHouse but, this whole endeavor to preload font fonts per Chrome's suggestion as upped my first meaningful paint to 3620ms from 2650ms and lower my lighthouse score from 88 to 79. – wildair Jun 12 '18 at 21:27
  • I also tried combining the tags like this: Which helped a little bit but, in trying to optimize I have inadvertently slowed everything down... – wildair Jun 12 '18 at 21:27

3 Answers3

44

The correct way to preload a font would be by adding both a preload link and a stylesheet. A simplified example, based on MDN is as follows:

<head>
  <meta charset="utf-8">
  <title>Preloading fonts</title>

  <link rel="preload" href="https://fonts.googleapis.com/css?family=Roboto&display=swap" as="style">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto&display=swap">
</head>

<body>
</body>

In the above example, the preload link will send a request to fetch the font regardless of it being installed or not on the client and then use the stylesheet link to properly load and use it.

preload is more of a way to tell the browser that a resource will probably be needed, so that it will be requested regardless and then, if you need it or decide to use it, you can.

Further information
Angelos Chalaris
  • 6,611
  • 8
  • 49
  • 75
37

It is recommended that you preconnect, then preload, and then finally load as follows:

<link rel='preconnect' href='https://fonts.gstatic.com' crossorigin>
<link rel='preload' as='style' href='https://fonts.googleapis.com/css2?family=Open+Sans|Roboto:wght@700&display=swap'>
<link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Open+Sans|Roboto:wght@700&display=swap'>

You cannot just preconnect and/or preload, you still need to load as usual. Then you just specify any font weight that is not the default for that given font by using :wght@700, for example. Between successive fonts you put the pipe | sign.

Michael Moriarty
  • 831
  • 13
  • 16
1

google always serve font from same domain fonts.gstatic.com

so preload it before adding style.

<link rel="preconnect" href="https://fonts.gstatic.com/">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto:700">

Khazi Afzal
  • 121
  • 2
  • 3
  • What you say is ambiguous. The 'preconnect' is to give your website a heads up and gets it connected to Google. The preload that you are missing indicates what you will be loading. And finally, you load the font(s) as you indicated using the api. – Michael Moriarty Jul 30 '23 at 18:29