Skip to content

Commit

Permalink
Script loader: enable fetchpriority attribute in the `wp_preload_reso…
Browse files Browse the repository at this point in the history
…urces` filter.

Add `fetchpriority` to the attributes accepted by the `wp_preload_resources` filter. Developers can now use this filter to set fetchpriority for resources being preloaded.

Props nihar007, luboslives, tabrisrp.
Fixes #58510.



git-svn-id: https://develop.svn.wordpress.org/trunk@57789 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
adamsilverstein committed Mar 7, 2024
1 parent da1e423 commit a38f5a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/wp-includes/general-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -3579,21 +3579,23 @@ function wp_preload_resources() {
* Filters domains and URLs for resource preloads.
*
* @since 6.1.0
* @since 6.6.0 Added the `$fetchpriority` attribute.
*
* @param array $preload_resources {
* Array of resources and their attributes, or URLs to print for resource preloads.
*
* @type array ...$0 {
* Array of resource attributes.
*
* @type string $href URL to include in resource preloads. Required.
* @type string $as How the browser should treat the resource
* (`script`, `style`, `image`, `document`, etc).
* @type string $crossorigin Indicates the CORS policy of the specified resource.
* @type string $type Type of the resource (`text/html`, `text/css`, etc).
* @type string $media Accepts media types or media queries. Allows responsive preloading.
* @type string $imagesizes Responsive source size to the source Set.
* @type string $imagesrcset Responsive image sources to the source set.
* @type string $href URL to include in resource preloads. Required.
* @type string $as How the browser should treat the resource
* (`script`, `style`, `image`, `document`, etc).
* @type string $crossorigin Indicates the CORS policy of the specified resource.
* @type string $type Type of the resource (`text/html`, `text/css`, etc).
* @type string $media Accepts media types or media queries. Allows responsive preloading.
* @type string $imagesizes Responsive source size to the source Set.
* @type string $imagesrcset Responsive image sources to the source set.
* @type string $fetchpriority Fetchpriority value for the resource.
* }
* }
*/
Expand Down Expand Up @@ -3641,7 +3643,7 @@ function wp_preload_resources() {
}

// Ignore non-supported attributes.
$non_supported_attributes = array( 'as', 'crossorigin', 'href', 'imagesrcset', 'imagesizes', 'type', 'media' );
$non_supported_attributes = array( 'as', 'crossorigin', 'href', 'imagesrcset', 'imagesizes', 'type', 'media', 'fetchpriority' );
if ( ! in_array( $resource_key, $non_supported_attributes, true ) && ! is_numeric( $resource_key ) ) {
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/tests/general/wpPreloadResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ public function data_preload_resources() {
),
),
),
'fetchpriority' => array(
'expected' => "<link rel='preload' href='https://example.com/image.jpg' as='image' fetchpriority='high' />\n",
'resources' => array(
array(
'href' => 'https://example.com/image.jpg',
'as' => 'image',
'fetchpriority' => 'high',
),
),
),
);
}
}

0 comments on commit a38f5a1

Please sign in to comment.