* Avoid duplicate preload tags.

* Add changelog entry.
This commit is contained in:
Jeff Stieler 2021-05-17 11:42:12 -06:00 committed by GitHub
parent f5309f449d
commit 8d2bd3bb2f
2 changed files with 31 additions and 12 deletions

View File

@ -106,6 +106,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
- Fix: Correct the left position of transient notices when the new nav is used. #6914
- Fix: Exclude WC Shipping for store that are only offering downloadable products #6917
- Fix: SelectControl focus and de-focus bug #6906
- Fix: Multiple preload tag output bug. #6998
- Tweak: Only fetch remote payment gateway recommendations when opted in #6964
- Update: Task list component with new Experimental Task list. #6849
- Update: Experimental task list import to the experimental package. #6950

View File

@ -43,6 +43,16 @@ class Loader {
*/
protected static $required_capability = null;
/**
* An array of dependencies that have been preloaded (to avoid duplicates).
*
* @var array
*/
protected $preloaded_dependencies = array(
'script' => array(),
'style' => array(),
);
/**
* Get class instance.
*/
@ -62,7 +72,7 @@ class Loader {
add_action( 'init', array( __CLASS__, 'define_tables' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'inject_wc_settings_dependencies' ), 14 );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts' ), 15 );
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ), 15 );
// Old settings injection.
add_filter( 'woocommerce_components_settings', array( __CLASS__, 'add_component_settings' ) );
// New settings injection.
@ -250,7 +260,7 @@ class Loader {
* @param string $file File name (without extension).
* @return string complete asset filename.
*
* @throws Exception Throws an exception when a readable asset registry file cannot be found.
* @throws \Exception Throws an exception when a readable asset registry file cannot be found.
*/
public static function get_script_asset_filename( $script_path_name, $file ) {
$minification_supported = Features::exists( 'minified-js' );
@ -676,7 +686,7 @@ class Loader {
/**
* Loads the required scripts on the correct pages.
*/
public static function load_scripts() {
public function load_scripts() {
if ( ! self::is_admin_or_embed_page() ) {
return;
}
@ -700,7 +710,7 @@ class Loader {
}
// Preload our assets.
self::output_header_preload_tags();
$this->output_header_preload_tags();
}
/**
@ -713,11 +723,19 @@ class Loader {
* @param string $type Dependency type - 'script' or 'style'.
* @param array $allowlist Optional. List of allowed dependency handles.
*/
public static function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
if ( ! empty( $allowlist ) && ! in_array( $dependency->handle, $allowlist, true ) ) {
public function maybe_output_preload_link_tag( $dependency, $type, $allowlist = array() ) {
if (
(
! empty( $allowlist ) &&
! in_array( $dependency->handle, $allowlist, true )
) ||
in_array( $dependency->handle, $this->preloaded_dependencies[ $type ], true )
) {
return;
}
$this->preloaded_dependencies[ $type ][] = $dependency->handle;
$source = $dependency->ver ? add_query_arg( 'ver', $dependency->ver, $dependency->src ) : $dependency->src;
echo '<link rel="preload" href="', esc_url( $source ), '" as="', esc_attr( $type ), '" />', "\n";
@ -732,7 +750,7 @@ class Loader {
* @param string $type Dependency type - 'script' or 'style'.
* @param array $allowlist Optional. List of allowed dependency handles.
*/
public static function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
public function output_header_preload_tags_for_type( $type, $allowlist = array() ) {
if ( 'script' === $type ) {
$dependencies_of_type = wp_scripts();
} elseif ( 'style' === $type ) {
@ -753,11 +771,11 @@ class Loader {
$sub_dependency = $dependencies_of_type->query( $sub_dependency_handle, 'registered' );
if ( $sub_dependency ) {
self::maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
$this->maybe_output_preload_link_tag( $sub_dependency, $type, $allowlist );
}
}
self::maybe_output_preload_link_tag( $dependency, $type, $allowlist );
$this->maybe_output_preload_link_tag( $dependency, $type, $allowlist );
}
}
@ -766,7 +784,7 @@ class Loader {
*
* See: https://macarthur.me/posts/preloading-javascript-in-wordpress
*/
public static function output_header_preload_tags() {
public function output_header_preload_tags() {
$wc_admin_scripts = array(
WC_ADMIN_APP,
'wc-components',
@ -781,10 +799,10 @@ class Loader {
);
// Preload styles.
self::output_header_preload_tags_for_type( 'style', $wc_admin_styles );
$this->output_header_preload_tags_for_type( 'style', $wc_admin_styles );
// Preload scripts.
self::output_header_preload_tags_for_type( 'script', $wc_admin_scripts );
$this->output_header_preload_tags_for_type( 'script', $wc_admin_scripts );
}
/**