-
Notifications
You must be signed in to change notification settings - Fork 9
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
Perf: improve Google Web Font performance #2639
Conversation
f38cd07
to
933cd57
Compare
I've been looking at this. Have a few questions, writing them up as a review comment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed in my browser console (FF 134.0.1 64-bit on Linux) these warnings related to preloading:
I'm looking at the MDN docs for rel="preload"
, and specifically What types of content can be preloaded?:
font
: Font file.- ...
- ...
Note:
font
... preloading requires thecrossorigin
attribute to be set; see CORS-enabled fetches below.
Then if we look at that CORS-enabled fetches section:
When preloading resources that are fetched with CORS enabled (e.g. fetch(), XMLHttpRequest or fonts), special care needs to be taken to setting the crossorigin attribute on your element. The attribute needs to be set to match the resource's CORS and credentials mode, even when the fetch is not cross-origin.
As mentioned above, one interesting case where this applies is font files. Because of various reasons, these have to be fetched using anonymous-mode CORS (see Font fetching requirements).
I would hope Google allows CORS requests for fonts since this seems like a common use-case and clear-cut requirement, but I'm not sure?
It is worth adding the crossorigin="anonymous"
attribute to the tag and seeing if it gets rid of this warning. We use this elsewhere e.g. for Bootstrap CSS
Getting the same warnings as @thekaveman on Mac OS/Firefox and Mac OS/Chrome: Safari, renders the same (fallback font) but no error/warning message in console. |
c0e86a4
to
9ae7973
Compare
Thanks for the comments @machikoyasuda and @thekaveman! I re-tested using macOS/Firefox, macOS/Chrome, and macOS/Safari and found a few things (in addition to the
|
https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap is a CSS file that loads font files, so I don't think using |
The documentation linked by #2492 is pretty general and assumes you're loading actual font files. I found some resources more specific to loading resources from the Google Fonts API, which is what we're doing to load Roboto.
I think it could help to look at the network waterfall chart in the devtools before and after making changes to see if changes are working as intended. |
Thanks @angela-tran! I'm still looking at this because even though the <link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet" type="text/css"> the page renders using Roboto. I just want to make sure that having both |
We should only need a single tag here right? This doesn't make sense to me. |
The first
The second |
Doh, thank you @angela-tran 🙏 |
@lalver1 What you're seeing makes sense with the docs on |
9ae7973
to
4cd9e73
Compare
After more testing (running scenarios 10 times and calculating the median) and reading more about the difference between In summary, we now warm up the connection to the Roboto web font using <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap"
rel="stylesheet" type="text/css"> The Largest Contentful Paint (LCP) (sec) is 0.13 and we see no warnings in the browser's console. There are several reasons (informed by article 1, and article 2) why I ended up going this route:
<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap" rel="stylesheet" type="text/css"> LCP (sec): 0.17
My only remaining concern is that Chrome's Lighthouse analysis gives
|
using preconnect.
for the web font to download.
4cd9e73
to
fc6bc19
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look good to me! Thanks for the links to those articles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested this locally on Mac/Safari, Chrome, Firefox. 👍
Closes #2492
This PR audits and improves how the Google Web Font is loaded.
Background
In
base.html
, the external font (Roboto) is loaded first, and the local font (Public Sans) is loaded after. Since there are no conflicts, Roboto takes precedence. Then, Roboto is set for--bs-font-sans-serif
so it is used throughout the site. The local font is only used for login.gov buttons.Audit
font-display: swap
andpreload
. Lighthouse reports an improvement in performance from 92 to 97 when using these attributes on<link>
.system-ui
which is a good fallback.