Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Output resource hinting only when using cart/checkout blocks on cart/…
Browse files Browse the repository at this point in the history
…checkout pages #7216
  • Loading branch information
mikejolley committed Oct 11, 2022
1 parent fdba4c4 commit e37cb98
Showing 1 changed file with 72 additions and 27 deletions.
99 changes: 72 additions & 27 deletions src/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 e37cb98

Please sign in to comment.