| @@ -24,11 +24,36 @@ | ||
| 24 | 24 | // on a single origin to avoid emitting duplicate @font-face rules. |
| 25 | 25 | add_filter( 'wp_theme_json_data_theme', [ $self, 'register_page_fonts' ] ); |
| 26 | 26 | // Remote fallback for fonts not yet downloaded locally. |
| 27 | 27 | add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_remote_fallback' ], 999 ); |
| 28 | + // Metric-adjusted local faces, so the pre-swap render is the right size. | |
| 29 | + add_action( 'wp_enqueue_scripts', [ $self, 'enqueue_fallback_faces' ], 5 ); | |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | /** |
| 33 | + * Emit the metric-adjusted fallback @font-face rules for this page's fonts. | |
| 34 | + * | |
| 35 | + * These reference locally installed system fonts via local() — nothing is | |
| 36 | + * downloaded. Their only job is to make the fallback render occupy exactly the | |
| 37 | + * space the real font will, so the swap doesn't shift the layout. | |
| 38 | + */ | |
| 39 | + public function enqueue_fallback_faces() { | |
| 40 | + $fonts = $this->page_fonts(); | |
| 41 | + if ( empty( $fonts ) ) { | |
| 42 | + return; | |
| 43 | + } | |
| 44 | + | |
| 45 | + $css = FontStack::get_fallback_face_css( $fonts ); | |
| 46 | + if ( '' === $css ) { | |
| 47 | + return; | |
| 48 | + } | |
| 49 | + | |
| 50 | + wp_register_style( 'ablocks-font-fallbacks', false, [], ABLOCKS_VERSION ); | |
| 51 | + wp_enqueue_style( 'ablocks-font-fallbacks' ); | |
| 52 | + wp_add_inline_style( 'ablocks-font-fallbacks', $css ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 31 | 56 | * Page fonts, resolved once per request. |
| 32 | 57 | */ |
| 33 | 58 | protected function page_fonts() { |
| 34 | 59 | return FontCollector::get_page_fonts(); |
| @@ -131,6 +156,30 @@ | ||
| 131 | 156 | } |
| 132 | 157 | |
| 133 | 158 | $url = 'https://fonts.googleapis.com/css2?family=' . implode( '&family=', $family_strings ) . '&display=swap'; |
| 134 | 159 | wp_enqueue_style( 'ablocks-google-fonts', esc_url( $url ), [], null ); |
| 160 | + | |
| 161 | + // Warm up the font connections early so the render-blocking stylesheet + | |
| 162 | + // its font files resolve faster (helps FCP/LCP). Only added when a remote | |
| 163 | + // font is actually loaded. | |
| 164 | + add_filter( 'wp_resource_hints', [ $this, 'preconnect_google_fonts' ], 10, 2 ); | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Add preconnect hints for the Google Fonts origins. | |
| 169 | + * | |
| 170 | + * @param array $hints Resource hint URLs/attributes for this relation. | |
| 171 | + * @param string $relation Current relation type (preconnect, dns-prefetch, …). | |
| 172 | + * @return array | |
| 173 | + */ | |
| 174 | + public function preconnect_google_fonts( $hints, $relation ) { | |
| 175 | + if ( 'preconnect' !== $relation ) { | |
| 176 | + return $hints; | |
| 177 | + } | |
| 178 | + $hints[] = [ 'href' => 'https://fonts.googleapis.com' ]; | |
| 179 | + $hints[] = [ | |
| 180 | + 'href' => 'https://fonts.gstatic.com', | |
| 181 | + 'crossorigin' => 'anonymous', | |
| 182 | + ]; | |
| 183 | + return $hints; | |
| 135 | 184 | } |
| 136 | 185 | } |