-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
audit: Web Font requests take a long time and push out FCP #3107
Comments
@addyosmani just to make sure it's clear. I add CSS.fontsUpdated listener to capture webfonts. If the webfonts loaded after FCP the audit fails. We recommend font-display when audit fails and fonts load from the same origin? So if I host my css on my cdn (akamai) and fonts are loaded from there as well than font-display will have no effect? (TIL) |
Okay so the typical bad webfont case that we want to flag is:
So to find these cases, we want to consider fonts who are important to the rendering of the page. And if they were downloading before FMP then they probably were "blocking" for FMP. So we'll want to flag all of these. Their "cost" to the load would probably be from the font network request "queuing" time until its finished. Queuing will be close to "start time" so we could use that if its painful. Font CDNsFont CDN's are tricky because I think we'll still want to flag GoogleFonts/Typekit but just recommend a different course of action. recommendations
|
how one would measure if |
I tried a few things but it's pretty hard to figure this one out. I can find when a font request is finished and if it's < First Interactive it's probably blocking? I've set a simple test page with fonts loading from google cdn.
|
I think all we're actually looking for here are font requests that finished before FCP whose initiator was the parser or a blocking stylesheet? |
Mmh looked like fcp was lower than the webfont timestamp as other content was painted already. I'll do another check to be sure. Only taking fonts from blocking stylesheets sounds awesome. Thanks for the pointers |
I think there's a little more to it. There is often a gap between the end of the font download and when it's painted. (Though maybe this isn't too huge.) This time would be included in the benefit of using swap/optional. One idea is using fontfaceobserver (or whatever that web platform API is called) to find out when each font is "loaded" But in the meantime we could just look at network time |
Would |
I found a few issues with this lighthouse when trying to figure this one out. When creating a demo page <html>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<body>
<script>
document.fonts.ready.then(() => {
window.__FONTSREADY = performance.now();
});
</script>
<style>
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: url(https://fonts.gstatic.com/s/roboto/v18/oMMgfZMQthOryQo9n22dcuvvDin1pK8aKteLpeZ5c0A.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215;
}
span {
font-family: "Roboto";
}
</style>
<span>Font font font</span>
<p>no special font</p>
</body>
</html> I get these values: So fontsready is the Code that's injected by lighthouse function getFonts() {
return document.fonts.ready.then(() => {
return performance.now()
});
} Devtoolslog: https://drive.google.com/open?id=0B3dMZOz0yvVQZDlNVDRtT0RWR00 |
To schedule some JS at the beginning of the page load you can use lighthouse/lighthouse-core/gather/driver.js Lines 208 to 217 in 76e51f3
If there aren't cases where its a significant cost though (and it's pretty much always fixed at ~100ms) I'd be in favor of keeping it simple and just reporting network savings. I think others disagree on that point though. I think we need firmer definition of what we're highlighting here. That demo page wouldn't trigger any warning as far as I can tell since FCP was not actually pushed out at all. If we're warning about all fonts that were required, not just blocking ones, then it seems like we should move it into best practices for just missing |
@patrickhulce correct, the demo page would not as it doesn't have a stylesheet that introduces the font face. Sometimes it can take a while to load. Also FCP is calculated pretty fast as some content is painted but not all so I don't know how relevant FCP is for fonts
will make my FCP = font loading (1.5s) which means fonts block FCP. What I'm trying to say that not all websites suffer from font blocking but of course it's not a best practice as their are many ways to do delayed loading |
@wardpeet let's chat on video for like 30min before you get too deep here. Hopefully we can do that to avoid too much back-n-forth later. :) |
Use
font-display: optional
(orswap
) - if you can’t load a font fast, just use a fallback until the next page load.One thing to be careful with:
font-display
doesn't apply for CDN fonts like those loaded from Google Fonts or TypeKit. Should we only apply this recommendation to Web Fonts loaded from the origin?The text was updated successfully, but these errors were encountered: