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

Commit

Permalink
Remove wc-settings from Mini Cart block dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Mar 9, 2023
1 parent 01efca1 commit 97d12ef
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 46 deletions.
9 changes: 4 additions & 5 deletions assets/js/blocks/mini-cart/frontend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { getSetting } from '@woocommerce/settings';
import preloadScript from '@woocommerce/base-utils/preload-script';
import lazyLoadScript from '@woocommerce/base-utils/lazy-load-script';
import getNavigationType from '@woocommerce/base-utils/get-navigation-type';
Expand All @@ -23,10 +22,10 @@ window.addEventListener( 'load', () => {
return;
}

const dependencies = getSetting(
'mini_cart_block_frontend_dependencies',
{}
) as Record< string, dependencyData >;
const dependencies = window.wcBlocksMiniCartFrontendDependencies as Record<
string,
dependencyData
>;

// Preload scripts
for ( const dependencyHandle in dependencies ) {
Expand Down
4 changes: 2 additions & 2 deletions src/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function __construct( Api $asset_api ) {
*/
protected function init() {
add_action( 'init', array( $this, 'register_data_script' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 );
add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 1 );
add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 2 );
add_action( 'admin_print_footer_scripts', array( $this, 'enqueue_asset_data' ), 2 );
}

/**
Expand Down
113 changes: 74 additions & 39 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_
protected function initialize() {
parent::initialize();
add_action( 'wp_loaded', array( $this, 'register_empty_cart_message_block_pattern' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'enqueue_wc_settings' ), 1 );
// We need this action to run after the equivalent in AssetDataRegistry.
add_action( 'wp_print_footer_scripts', array( $this, 'print_lazy_load_scripts' ), 3 );
}

/**
Expand Down Expand Up @@ -150,45 +153,6 @@ protected function enqueue_data( array $attributes = [] ) {
);
}

$script_data = $this->asset_api->get_script_data( 'build/mini-cart-component-frontend.js' );

$num_dependencies = count( $script_data['dependencies'] );
$wp_scripts = wp_scripts();

for ( $i = 0; $i < $num_dependencies; $i++ ) {
$dependency = $script_data['dependencies'][ $i ];

foreach ( $wp_scripts->registered as $script ) {
if ( $script->handle === $dependency ) {
$this->append_script_and_deps_src( $script );
break;
}
}
}

$payment_method_registry = Package::container()->get( PaymentMethodRegistry::class );
$payment_methods = $payment_method_registry->get_all_active_payment_method_script_dependencies();

foreach ( $payment_methods as $payment_method ) {
$payment_method_script = $this->get_script_from_handle( $payment_method );

if ( ! is_null( $payment_method_script ) ) {
$this->append_script_and_deps_src( $payment_method_script );
}
}

$this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array(
'src' => $script_data['src'],
'version' => $script_data['version'],
'translations' => $this->get_inner_blocks_translations(),
);

$this->asset_data_registry->add(
'mini_cart_block_frontend_dependencies',
$this->scripts_to_lazy_load,
true
);

$this->asset_data_registry->add(
'displayCartPricesIncludingTax',
$this->display_cart_prices_including_tax,
Expand Down Expand Up @@ -241,6 +205,77 @@ protected function enqueue_data( array $attributes = [] ) {
do_action( 'woocommerce_blocks_cart_enqueue_data' );
}

/**
* Function to enqueue `wc-settings` script and dequeue it later on so when
* AssetDataRegistry runs, it appears enqueued- This allows the necessary
* data to be printed to the page.
*/
public function enqueue_wc_settings() {
// Return early if another block has already enqueued `wc-settings`.
if ( wp_script_is( 'wc-settings', 'enqueued' ) ) {
return;
}
// We are lazy-loading `wc-settings`, but we need to enqueue it here so
// AssetDataRegistry knows it's going to load.
wp_enqueue_script( 'wc-settings' );
// After AssetDataRegistry function runs, we dequeue `wc-settings`.
add_action( 'wp_print_footer_scripts', array( $this, 'dequeue_wc_settings' ), 4 );
}

/**
* Function to dequeue `wc-settings` script.
*/
public function dequeue_wc_settings() {
wp_dequeue_script( 'wc-settings' );
}

/**
* Prints the variable containing information about the scripts to lazy load.
*/
public function print_lazy_load_scripts() {
$script_data = $this->asset_api->get_script_data( 'build/mini-cart-component-frontend.js' );

$num_dependencies = count( $script_data['dependencies'] );
$wp_scripts = wp_scripts();

for ( $i = 0; $i < $num_dependencies; $i++ ) {
$dependency = $script_data['dependencies'][ $i ];

foreach ( $wp_scripts->registered as $script ) {
if ( $script->handle === $dependency ) {
$this->append_script_and_deps_src( $script );
break;
}
}
}

$payment_method_registry = Package::container()->get( PaymentMethodRegistry::class );
$payment_methods = $payment_method_registry->get_all_active_payment_method_script_dependencies();

foreach ( $payment_methods as $payment_method ) {
$payment_method_script = $this->get_script_from_handle( $payment_method );

if ( ! is_null( $payment_method_script ) ) {
$this->append_script_and_deps_src( $payment_method_script );
}
}

$this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array(
'src' => $script_data['src'],
'version' => $script_data['version'],
'translations' => $this->get_inner_blocks_translations(),
);

$data = rawurlencode( wp_json_encode( $this->scripts_to_lazy_load ) );
$mini_cart_dependencies_script = "var wcBlocksMiniCartFrontendDependencies = JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );";

wp_add_inline_script(
'wc-mini-cart-block-frontend',
$mini_cart_dependencies_script,
'before'
);
}

/**
* Returns the script data given its handle.
*
Expand Down

0 comments on commit 97d12ef

Please sign in to comment.