Skip to content

Conversation

@zaguiini
Copy link
Member

@zaguiini zaguiini commented Apr 6, 2022

What?

Do not trigger an error if trying to enqueue a font family that was already enqueued.

Why?

It's super annoying. If the font family is already enqueued, let's just move on with our lives.

When implementing a web font scanning logic for example, the same font might be referenced by multiple blocks, and having to check whether the font was already enqueued before just to avoid the warning is redundant -- we do have guards inside the enqueueing method.

Testing Instructions

add_action(
  'after_setup_theme',
  function() {
    wp_register_webfont(
      array(
        'font-family'  => 'Roboto',
        'font-style'   => 'normal',
        'font-stretch' => 'normal',
        'font-weight'  => '400',
        'src'          => get_theme_file_uri( '/assets/fonts/Roboto-regular.ttf' ),
      )
    );

    wp_enqueue_webfont( 'Roboto' );
    wp_enqueue_webfont( 'Roboto' );
  }
);

Notice we're calling wp_enqueue_webfont twice. When opening the site, it should not yell at you because you called enqueue twice for the same family.

@jeyip
Copy link
Contributor

jeyip commented Apr 7, 2022

Testing

Requirements

  • php notice no longer appears when enqueueing a font twice
       wp_register_webfont(
		array(
			'font-family'  => 'Roboto',
			'font-style'   => 'normal',
			'font-stretch' => 'normal',
			"font-weight" => "900",
			'src'          => array( 'file:./assets/fonts/Roboto-Regular.ttf' ),
		)
	);

	wp_enqueue_webfont('Roboto');
	wp_enqueue_webfont('Roboto');

@simison
Copy link
Member

simison commented Apr 7, 2022

Should we instead expect devs to use something along the lines of wp_script_is to check if font is enqueued, before re-enqueuing it?

@hellofromtonya
Copy link
Contributor

Good idea @zaguiini! I can see how this would happen and be super annoying. There's no harm in attempting to enqueue more than 1 time. The API knows if it's already enqueued and can return a positive indication to flag the state. No need for the warning.

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Thanks for flagging this annoyance. I agree in removing it and returning true as it's already enqueued. LGTM 👍

@zaguiini
Copy link
Member Author

zaguiini commented Apr 7, 2022

I think we can have both, @simison. The warning is useless as trying to re-enqueue is a noop.

@zaguiini zaguiini merged commit ba8b6ef into trunk Apr 7, 2022
@zaguiini zaguiini deleted the try/do-not-trigger-warning-on-already-enqueued-font-families branch April 7, 2022 13:53
@github-actions github-actions bot added this to the Gutenberg 13.0 milestone Apr 7, 2022
@bph bph added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Dev Note Requires a developer note for a major WordPress release cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants