Fonts chosen under a classic theme keep loading after switching to a block theme

Aperta
#53,167 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
performance

Direzione di ricerca

Start by reading Jetpack_Fonts::maybe_render_fonts() and get_font_css(); the issue does not name their file or any tests. Check how the method handles empty CSS and the jetpack_fonts_render_without_rules filter, then verify that themes without applicable font rules emit neither the loader nor the style while themes with rules still do.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug Jetpack Agent Backlog
Impacted plugin

Wpcomsh

Quick summary

If a user uses a classic theme, and adds custom fonts in the customizer, then switches to a block theme, those added fonts will still be loaded for visitors, even if they aren't used. And the site admin cannot remove the fonts as the block theme now hides access to the customizer.

Custom Fonts stores the chosen fonts in the site option jetpack_fonts (selected_fonts), whatever theme is active. Jetpack_Fonts::maybe_render_fonts() prints the WebFontConfig loader and webfont.js whenever that option has fonts. If the active theme has no font annotations (any block theme, for example), the generated font CSS is empty, so nothing on the page uses the fonts. But webfont.js still downloads them on every page view. On a block theme the Customizer isn't in the Appearance menu, so site owners can't clear the setting themselves.

Steps to reproduce
  1. On a classic theme with font annotations, choose a heading and a body font in Customizer → Fonts, then publish.
  2. Switch to Twenty Twenty-Five.
  3. View the front-end page source.

Expected: no Custom Fonts output, because the theme has no rules to apply the fonts.
Actual: a WebFontConfig block and webfont.js load both fonts (through fonts-api.wp.com on WordPress.com), and <style id="jetpack-custom-fonts-css"> is empty.

Site owner impact

Fewer than 20% of the total website/platform users

Severity

Minor

What other impact(s) does this issue have?

No response

If a workaround is available, please outline it here.
Cause
  • maybe_render_fonts() checks only get_fonts() (plus Customizer preview and AMP) before calling output_webfont_loader().
  • get_font_css() returns no rules for themes without annotations, but the loader runs anyway.
  • Nothing clears or ignores selected_fonts when the theme changes. (gutenberg-integration.php already skips block themes for editor styles.)
Impact

Unused font downloads plus an extra script on every page view, on any site that picked fonts under an annotated theme and later moved to a theme without annotations. Reported by an Atomic customer on Twenty Twenty-Five, who had Arimo and Lato loading for nothing.

Proposed fix

In maybe_render_fonts(), build the font CSS first, and return before printing the loader if it is empty. See the comments below for details, and for why no data backfill is needed.

public function maybe_render_fonts( $force = false ) {
	if ( ( is_customize_preview() && ! $force ) || ! $this->get_fonts() || ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ) {
		return;
	}

	$css = $this->get_font_css();
	// Without theme rules for the chosen fonts, nothing on the page uses them.
	if ( '' === trim( $css ) && ! apply_filters( 'jetpack_fonts_render_without_rules', false ) ) {
		return;
	}

	$webfont_options = $this->get_webfont_options();
	if ( count( (array) $webfont_options ) > 0 ) {
		$this->output_webfont_loader( $webfont_options );
	}

	echo '<style id="jetpack-custom-fonts-css">' . $css . "</style>\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

Why this, and why no backfill:

  • The check runs at page render, so it fixes every affected site as soon as it deploys. No option data needs migrating.
  • Deleting selected_fonts in bulk would be worse. It can't be undone, and a site that switches back to its classic theme would lose fonts it chose deliberately. With the guard, those picks simply start working again.
  • It's precise. It skips output only when no rule applies to any chosen font, whatever the theme type. It also covers classic themes without annotations, which have the same waste.

One risk to check first: someone might have picked a font in Custom Fonts and then used it in their own Additional CSS, relying on the loader to fetch it. The guard would stop loading that font. The jetpack_fonts_render_without_rules filter is an escape hatch for those sites. Before shipping, it's worth sampling how many sites have selected_fonts set, no annotation rules, and Additional CSS that mentions one of those family names.

Workaround

Delete the stale selection: wp option patch delete jetpack_fonts selected_fonts.

Platform (Simple and/or Atomic)

Atomic

Lingua principale
PHP
Stelle
1.9k
Fork
913
Merge medio
1g 18h
PR unite (30g)
809

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Automattic/jetpack

Tutte le issue di Automattic/jetpack

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.