Expand externalization of data: URLs in @font-face rules #2079
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.
Twenty Nineteen has quite a bit of CSS, and part of it is due to the
NonBreakingSpaceOverride
font which is inlined as adata:
URL. While this font was temporarily removed from the frontend styles in WordPress/twentynineteen#623 it was added back in WordPress/twentynineteen#670 as @kjellr notes in WordPress/twentynineteen#623 (comment):In AMP it turns out to be a big deal, because of the 50KB budget for
style[amp-custom]
. Thedata:
-URL inlined WOFF2 and WOFF files account for 3KB, which is 6% of the budget. This is a lot especially for something that definitely not required, per above.The AMP plugin already has built-in support for externalizing
data:
URLs for font files in the case of Dashicons. That externalization depended on one of the fontsrc
URLs to be an external file so that it could guess where to find the external file version of thedata:
-encoded font. For example,dashicons.css
has:Based on this, the plugin can successfully guess that woff font encoded in the
data:
URL would probably be located at../fonts/dashicons.woff
since the TrueType version is located at../fonts/dashicons.ttf
.That's all well and good. But what if there are no non-
data:
URLs defined? This is the case for Twenty Nineteen. It contains:The AMP plugin doesn't know where to locate the woff2 and woff files.
It turns out that even though Twenty Nineteen doesn't reference these files externally, they are present in the theme at:
fonts/NonBreakingSpaceOverride.woff2
fonts/NonBreakingSpaceOverride.woff
With this in mind, the plugin can expand its guessing of the external file locations based on the name in the
font-family
.As an added touch for performance, when a
data:
URL is converted to an external URL, the@font-face
gets afont-display: swap
. This prevents the font's loading from blocking the rendering of the text.Before:
style.css
at 42KBAfter:
style.css
at 39KBSee #1492.