Skip to content
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

Closed
wants to merge 6 commits into from
Closed
17 changes: 17 additions & 0 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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' ) );
}

/**
Expand Down Expand Up @@ -291,4 +294,18 @@ public function generate_styles() {

return $styles;
}

/**
* Preload webfonts to improve performance
*
* @return void
*/
function preload_webfonts() {
foreach ( $this->get_fonts() as $font ) {
foreach( $font['src'] as $src ) {
$srcAsArray = explode( '.', $src );
echo '<link rel="preload" href="' . $src . '" as="font" type="font/' . end( $srcAsArray ) . '" crossorigin />';
}
}
}
}