Make WordPress Core


Ignore:
Timestamp:
10/16/2025 11:30:56 PM (4 months ago)
Author:
westonruter
Message:

Editor: Opt to dequeue assets enqueued in hidden blocks, rather than to enqueue assets for non-hidden blocks.

This eliminates constant emptying out of the queues for styles, scripts, and script modules before rendering each block. This ensures that wp_script_is()/wp_style_is() will return true for assets that are actually enqueued. The WP_Script_Modules::$queue member which was made public in [60930] is now made private in favor of a WP_Script_Modules::get_queue() method, since there is no need to clear out the queue before rendering each block and restore after the rendering is complete.

Finally, as a very special case for unusual blocks which contain wp_head(), a check is done to see if the wp_enqueue_scripts action occurred during the rendering of a block; if so, then no assets will be dequeued even if no markup is rendered in the block, since it may be that a script was enqueued for the footer and not the head.

Developed in https://github.com/WordPress/wordpress-develop/pull/10252

Follow-up to [60930].

Props westonruter, dd32, peterwilsoncc, nikunj8866, krupajnanda.
Fixes #63676.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-script-modules.php

    r60931 r60951  
    2929     * @var string[]
    3030     */
    31     public $queue = array();
     31    private $queue = array();
    3232
    3333    /**
     
    139139
    140140    /**
     141     * Gets IDs for queued script modules.
     142     *
     143     * @since 6.9.0
     144     *
     145     * @return string[] Script module IDs.
     146     */
     147    public function get_queue(): array {
     148        return $this->queue;
     149    }
     150
     151    /**
    141152     * Checks if the provided fetchpriority is valid.
    142153     *
     
    238249     */
    239250    public function dequeue( string $id ) {
    240         $this->queue = array_diff( $this->queue, array( $id ) );
     251        $this->queue = array_values( array_diff( $this->queue, array( $id ) ) );
    241252    }
    242253
Note: See TracChangeset for help on using the changeset viewer.