Change font-display: fallback
to font-display: swap
to improve percieved performance
#1607
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hypothesis
Changing
font-display: fallback
tofont-display: swap
will improve perceived performance for users.Reasons for Hypothesis
According to the specification:
So for
swap
, the browser will draw the text immediately (within 100ms) with a fallback font, then the browser has infinite time to swap the font out for the web font once loaded.With
fallback
the browser will draw the text immediately (within 100ms) with the fallback font. But the browser only has a finite period of time to swap to the web font once loaded. If the font isn't loaded in 3 seconds, the fallback font will be used for the lifetime of the page.Others have suggested changing
font-display
fromfallback
toswap
too. To quote from Addy Osmani:More discussion about this can be seen here, here and here.
Cloudflare Results
I tested both
font-display
settings using under different conditions, browsers and devices. All tests were run using WebPageTest, and the visual output of each test observed to see which completed in a way that is best for the user.Note: Tests on the
govuk-frontend-review-pr-*
domains link to third-party resources (CSS, JS) from Cloudflare and sometimes Google Fonts. The reliance on this third-party CSS could be the cause of some of the fluctuations in performance due to network congestion at the time the test is run.Chrome - 3G - Announcements
Observations
Fallback
renders pixels to the page 32ms beforeswap
, FOIT occurs.Swap
shows the web font at the same time as the page renders. In this casefallback
has better perceived performance.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Chrome - 3G - Applicant
Observations
fallback
renders pixels to the page beforeswap
, FOIT occurs.fallback
shows content beforeswap
.swap
loads and displays the web font 700ms quicker thanfallback
Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Chrome - 3G - Bank Holidays
Observations
fallback
renders pixels to the page before swap, FOIT occurs.fallback
renders content to the page quicker thanswap
(300ms). Web font forswap
loads 300ms slower thanfallback
.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Firefox - 3G - Announcements
Observations
fallback
renders pixels to the viewport, content to the page, and loads the web font 200ms quicker thanswap
.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Firefox - 3G - Applicant
Observations
FOIT observed when using
fallback
. It renders pixels and content to the viewport quicker (100ms). Web font also loads 100ms quicker.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Firefox - 3G - Bank Holidays
Observations
FOIT visible for 100ms for both.
fallback
renders pixels to the viewport 200ms quicker. Font loads and displays quicker forfallback
.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Moto G4 - Chrome - 3G - Announcements
Tested on a real device, based in Dulles, VA.
Observations
swap
page rendered extremely quickly once resources were loaded, within a single frame (~16ms). FOIT only visible for thefallback
for 100ms.Fallback
Swap
Perceived performance winner
swap
Full results
Full comparison results can be found here.
Moto G4 - Chrome - 3G - Applicant
Tested on a real device, based in Dulles, VA.
Observations
FOIT seen for first 100ms in both cases.
swap
renders pixels to the page some 500ms beforefallback
in this case. Page completion delayed due to the loading of the crown icon (not relevant).Fallback
Swap
Perceived performance winner
swap
Full results
Full comparison results can be found here.
Moto G4 - Chrome - 3G - Bank Holidays
Tested on a real device, based in Dulles, VA.
Observations
FOIT visible for around 200ms with
fallback
, and it renders pixels to the screen beforeswap
. Content is rendered to the viewport at a similar time for both cases. Same applies for the actual web font download.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Fastly Results
Sample pages taken from GOV.UK were tested to understand what the results were without the third-party dependencies. In this case the assets domain has also been removed which will remove the performance bottleneck when using HTTP/2.
HTTP/1.1 and HTTP/2 were tested to see how the protocol effected the results.
GOV.UK Homepage - HTTP/1.1 - 3G - Chrome
Observations
FOIT seen in the
fallback
version for 100ms, but not forswap
. Initial page render contains the fallback font forswap
, so perceived performance is better for this case. Actual font load is slower forswap
by 100ms.Fallback
Swap
Perceived performance winner
swap
Full results
Full comparison results can be found here.
GOV.UK Homepage - HTTP/2 - 3G - Chrome
Observations
FOIT seen with
fallback
but notswap
. Pixels rendered to the viewport 100ms quicker withfallback
, actual content rendered at the same time. Font loads in a similar time.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Start Page - HTTP/1.1 - 3G - Moto G4 - Chrome
Tested on a real device, based in Dulles, VA.
Observations
fallback
renders pixels to the viewport quicker, but FOIT is seen for 100ms. Content rendered 200ms quicker forswap
, but the web font loads 400ms slower.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Start Page - HTTP/2 - 3G - Moto G4 - Chrome
Tested on a real device, based in Dulles, VA.
Observations
fallback
renders pixels to the viewport quicker, but FOIT is seen for 300ms.swap
renders the fallback at the same time as it draws the page. Web font loads slightly quicker forswap
.Fallback
Swap
Perceived performance winner
fallback
Full results
Full comparison results can be found here.
Conclusion
From the test results above, many of the tests look to show better perceived performance when
fallback
is used. This is assuming that painting any pixel to the viewport is perceived as quicker (open to interpretation). From the tests run,fallback
was better for 10 out of 13 tests.Recommendation
Off the back of the results above my recommendation would be to keep the
font-display
setting atfallback
rather thanswap
, since it allows any type of pixels to be rendered to the viewport quicker.We should do a similar test to see if
font-display: optional
may be a better alternative for older devices, since it allows a browser to cancel the web font download completely if the connection is very slow.