Skip to content

Commit

Permalink
Prevent resource hinting when cart/checkout blocks are not in use (wo…
Browse files Browse the repository at this point in the history
…ocommerce#7364)

* Skip resource hinting on admin requests woocommerce#7215

* Output resource hinting only when using cart/checkout blocks on cart/checkout pages woocommerce#7216
  • Loading branch information
mikejolley authored and senadir committed Nov 12, 2022
1 parent 38caed3 commit 721eb02
Showing 1 changed file with 73 additions and 28 deletions.
101 changes: 73 additions & 28 deletions src/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function register_assets() {
* @return array URLs to print for resource hints.
*/
public function add_resource_hints( $urls, $relation_type ) {
if ( ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) ) {
if ( ! in_array( $relation_type, [ 'prefetch', 'prerender' ], true ) || is_admin() ) {
return $urls;
}

Expand All @@ -97,39 +97,72 @@ public function add_resource_hints( $urls, $relation_type ) {
return $urls;
}

$resources = [];
$is_block_cart = has_block( 'woocommerce/cart' );
$is_block_checkout = has_block( 'woocommerce/checkout' );

if ( 'prefetch' === $relation_type ) {
if ( ! $is_block_cart ) {
$resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'cart-frontend' ) );
}

if ( ! $is_block_checkout ) {
$resources = array_merge( $resources, $this->get_block_asset_resource_hints( 'checkout-frontend' ) );
}
$urls = array_merge(
$urls,
$this->get_prefetch_resource_hints()
);
}

if ( 'prerender' === $relation_type ) {
$urls = array_merge(
$urls,
array_map(
function( $src ) {
return array(
'href' => $src,
'as' => 'script',
);
},
array_unique( array_filter( $resources ) )
)
$this->get_prerender_resource_hints()
);
}

if ( 'prerender' === $relation_type && $is_block_cart ) {
$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : '';
if ( $checkout_page_url ) {
$urls[] = $checkout_page_url;
}
return $urls;
}

/**
* Get resource hints during prefetch requests.
*
* @return array Array of URLs.
*/
private function get_prefetch_resource_hints() {
$urls = [];

// Core page IDs.
$cart_page_id = wc_get_page_id( 'cart' );
$checkout_page_id = wc_get_page_id( 'checkout' );

// Checks a specific page (by ID) to see if it contains the named block.
$has_block_cart = $cart_page_id && has_block( 'woocommerce/cart', $cart_page_id );
$has_block_checkout = $checkout_page_id && has_block( 'woocommerce/checkout', $checkout_page_id );

// Checks the current page to see if it contains the named block.
$is_block_cart = has_block( 'woocommerce/cart' );
$is_block_checkout = has_block( 'woocommerce/checkout' );

if ( $has_block_cart && ! $is_block_cart ) {
$urls = array_merge( $urls, $this->get_block_asset_resource_hints( 'cart-frontend' ) );
}

if ( $has_block_checkout && ! $is_block_checkout ) {
$urls = array_merge( $urls, $this->get_block_asset_resource_hints( 'checkout-frontend' ) );
}

return $urls;
}

/**
* Get resource hints during prerender requests.
*
* @return array Array of URLs.
*/
private function get_prerender_resource_hints() {
$urls = [];
$is_block_cart = has_block( 'woocommerce/cart' );

if ( ! $is_block_cart ) {
return $urls;
}

$checkout_page_id = wc_get_page_id( 'checkout' );
$checkout_page_url = $checkout_page_id ? get_permalink( $checkout_page_id ) : '';

if ( $checkout_page_url ) {
$urls[] = $checkout_page_url;
}

return $urls;
Expand All @@ -148,7 +181,19 @@ private function get_block_asset_resource_hints( $filename = '' ) {
$script_data = $this->api->get_script_data(
$this->api->get_block_asset_build_path( $filename )
);
return array_merge( [ add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ], $this->get_script_dependency_src_array( $script_data['dependencies'] ) );
$resources = array_merge(
[ add_query_arg( 'ver', $script_data['version'], $script_data['src'] ) ],
$this->get_script_dependency_src_array( $script_data['dependencies'] )
);
return array_map(
function( $src ) {
return array(
'href' => $src,
'as' => 'script',
);
},
array_unique( array_filter( $resources ) )
);
}

/**
Expand Down

0 comments on commit 721eb02

Please sign in to comment.