incubator-pagespeed-mod
incubator-pagespeed-mod copied to clipboard
Proposal: Insert font-display:swap for Google Fonts CSS inlining
Lighthouse and other tools complain about google fonts not supporting font-display:swap. I've recently implemented a script which rewrites the inlined Font css, adding the font-display:swap to the style rules, if it is inlined. This gives immense improvement on webpagetest.org and Lighthouse.
As we inline the CSS we also have the possibility to rewrite the css when it is stored in the cache. I'd imagine having a content callback on the cache item (next to the header callback) and replacing the value with the rewritten CSS. The font-display value should be configurable on the filter or even on the link tag to make the configuration granular.
Warning: This will give an inconsistent user experience, first request will be with FOIT, next requests will be with FOUT, as we cannot rewrite external stylesheets
Thoughts?
This could also be applied to local fonts if changing the stylesheets is not easy. Looking at the profile trace and doing it by script immediately after the style loads seems to have a relatively little impact.
Hi @keesspoelstra
Well, you can use <link href="https://fonts.googleapis.com/css?family=Roboto&display=swap"
now.
@Lofesa it is lost when inlininig fonts.
@tomasdev I see, the inline option relies in a third party library ( in the code tree third_party/css_parser/ ) that don´t take account of the display attribute in the css for fonts. I think this library don´t know nothing about font-display.