-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Webfonts: Preload fonts in the head to stop a flash of unstyled content #39391
Changes from all commits
9a21788
16ac0d6
529103a
9f5fb53
4aab001
8dcf2b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,6 +35,11 @@ class WP_Webfonts { | |||||||||||||||
*/ | ||||||||||||||||
private $stylesheet_handle = ''; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Allowed file types | ||||||||||||||||
*/ | ||||||||||||||||
private static $allowed_types = array( 'woff', 'woff2', 'ttf', 'eot', 'otf' ); | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Init. | ||||||||||||||||
*/ | ||||||||||||||||
|
@@ -55,6 +60,9 @@ public function init() { | |||||||||||||||
|
||||||||||||||||
// Enqueue webfonts in the block editor. | ||||||||||||||||
add_action( 'admin_init', array( $this, 'generate_and_enqueue_editor_styles' ) ); | ||||||||||||||||
|
||||||||||||||||
// Preload the font files. | ||||||||||||||||
add_action( 'wp_head', array( $this, 'preload_webfonts' ) ); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
|
@@ -165,6 +173,7 @@ public function validate_font( $font ) { | |||||||||||||||
'font-feature-settings', | ||||||||||||||||
'font-variation-settings', | ||||||||||||||||
'line-gap-override', | ||||||||||||||||
'preload', | ||||||||||||||||
'size-adjust', | ||||||||||||||||
'src', | ||||||||||||||||
'unicode-range', | ||||||||||||||||
|
@@ -291,4 +300,24 @@ public function generate_styles() { | |||||||||||||||
|
||||||||||||||||
return $styles; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* Preload webfonts to improve performance | ||||||||||||||||
* | ||||||||||||||||
* @return void | ||||||||||||||||
*/ | ||||||||||||||||
public function preload_webfonts() { | ||||||||||||||||
foreach ( $this->get_fonts() as $font ) { | ||||||||||||||||
if ( ! empty( $font['preload'] ) && $font['preload'] ) { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is deeply nested causing the code to shift to the right. This can decrease readability. I'd suggest either breaking it up into private methods -or- converting this guard into a skip clause like this:
Suggested change
|
||||||||||||||||
foreach( $font['src'] as $src ) { | ||||||||||||||||
$src_as_array = explode( '.', $src ); | ||||||||||||||||
$file_type = end( $src_as_array ); | ||||||||||||||||
Comment on lines
+313
to
+314
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Micro-optimization suggestion:
Suggested change
Why? Results:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW This code is not needed if the preload functionality instead grabs the font srcs from the provider's |
||||||||||||||||
if ( ! in_array( $file_type, self::$allowed_types ) ) { | ||||||||||||||||
$file_type = ''; | ||||||||||||||||
} | ||||||||||||||||
echo '<link rel="preload" href="' . $src . '" as="font" type="font/' . $file_type . '" crossorigin />'; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Thinking about it, yes, this is a better approach to avoid redundancy. I agree that this method is a better fit for the local provider ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is rendering the HTML, the variables require escaping for security:
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
} |
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.
Since
preload
is not a valid CSS prop, I think it would be best if we added it to the// exceptions
section a few lines below, along withprovider
.We'd also need to add a check in the local-provider class to make sure that no CSS is generated for this:
gutenberg/lib/compat/wordpress-6.0/class-wp-webfonts-provider-local.php
Lines 193 to 197 in 9f5fb53