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

Don't load wc blocks data on every route. #3864

Closed

Conversation

budzanowski
Copy link
Contributor

@budzanowski budzanowski commented Feb 18, 2021

Fixes: #3748

There are a couple of connected but different things happening in this PR:

  1. Removal of BackCompatAssetRegistry: this was used for WC Admin <= v0.19.0. This is an 18 months old version so this should be safe to remove.
  2. Decoupling loading of blocks common data from the woocommerce_shared_settings filter. In the new approach, the common data is registered by each block ( previously checking if it was not already registered ) in AbstractBlock::enqueue_data. This is managed by the default render_callback. This ensures that the data creation function will be called only if Gutenberg will render a block.
  3. Deprecation of woocommerce_shared_settings

Testing:

This reorganizes how data required by blocks are loaded. Therefore this requires a rather thorough smoke testing.

The only truly visible item from the outside should be the filter deprecation. All other items are internal refactor and are skipped from the changelog.

Changelog

Filter woocommerce_shared_settings is now deprecated.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 18, 2021

Size Change: +1.52 kB (0%)

Total Size: 1.15 MB

Filename Size Change
build/active-filters-frontend.js 8.38 kB +36 B (0%)
build/active-filters.js 8.57 kB +49 B (+1%)
build/all-products-frontend.js 34.7 kB -15 B (0%)
build/all-products.js 36.4 kB -42 B (0%)
build/all-reviews.js 9.93 kB +39 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 3.37 kB +1 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/image--atomic-block-components/title.js 334 B -1 B (0%)
build/atomic-block-components/add-to-cart-frontend.js 9.23 kB +2 B (0%)
build/atomic-block-components/add-to-cart.js 7.68 kB -4 B (0%)
build/atomic-block-components/button-frontend.js 2.39 kB +2 B (0%)
build/atomic-block-components/button.js 841 B +1 B (0%)
build/atomic-block-components/category-list-frontend.js 469 B -1 B (0%)
build/atomic-block-components/category-list.js 478 B +2 B (0%)
build/atomic-block-components/image.js 1.24 kB +2 B (0%)
build/atomic-block-components/price-frontend.js 1.94 kB -2 B (0%)
build/atomic-block-components/price.js 1.97 kB +2 B (0%)
build/atomic-block-components/rating-frontend.js 521 B +1 B (0%)
build/atomic-block-components/rating.js 527 B +1 B (0%)
build/atomic-block-components/sale-badge-frontend.js 858 B +1 B (0%)
build/atomic-block-components/summary-frontend.js 898 B -22 B (-2%)
build/atomic-block-components/summary.js 902 B -23 B (-2%)
build/atomic-block-components/tag-list-frontend.js 467 B +2 B (0%)
build/atomic-block-components/title-frontend.js 1.35 kB -2 B (0%)
build/atomic-block-components/title.js 1.21 kB +2 B (0%)
build/attribute-filter-frontend.js 18.3 kB +21 B (0%)
build/attribute-filter.js 12.6 kB +44 B (0%)
build/blocks-checkout.js 17 kB -29 B (0%)
build/blocks.js 3.49 kB -1 B (0%)
build/cart-frontend.js 75.7 kB -7 B (0%)
build/cart.js 38.5 kB +72 B (0%)
build/checkout-frontend.js 80.4 kB +20 B (0%)
build/checkout.js 41.3 kB +23 B (0%)
build/featured-category.js 7.87 kB +49 B (+1%)
build/featured-product.js 10.1 kB +33 B (0%)
build/handpicked-products.js 7.56 kB +46 B (+1%)
build/price-filter-frontend.js 14.6 kB +21 B (0%)
build/price-filter.js 9.99 kB +38 B (0%)
build/price-format.js 1.35 kB +2 B (0%)
build/product-best-sellers.js 7.62 kB +41 B (+1%)
build/product-categories.js 3.23 kB -2 B (0%)
build/product-category.js 8.57 kB +51 B (+1%)
build/product-new.js 7.79 kB +40 B (+1%)
build/product-on-sale.js 8.19 kB +50 B (+1%)
build/product-search.js 3.64 kB +58 B (+2%)
build/product-tag.js 6.63 kB +49 B (+1%)
build/product-top-rated.js 7.76 kB +39 B (+1%)
build/products-by-attribute.js 8.55 kB +50 B (+1%)
build/reviews-by-category.js 12 kB +47 B (0%)
build/reviews-by-product.js 13.5 kB +44 B (0%)
build/reviews-frontend.js 9.57 kB +52 B (+1%)
build/single-product-frontend.js 38 kB +30 B (0%)
build/single-product.js 10.3 kB +25 B (0%)
build/style-rtl.css 18.9 kB +32 B (0%)
build/style.css 18.9 kB +24 B (0%)
build/vendors.js 418 kB +2 B (0%)
build/wc-blocks-data.js 6.97 kB -6 B (0%)
build/wc-blocks-middleware.js 1.1 kB +2 B (0%)
build/wc-blocks-registry.js 2.67 kB +1 B (0%)
build/wc-payment-method-cheque.js 817 B +1 B (0%)
build/wc-payment-method-stripe.js 12.7 kB +536 B (+4%)
build/wc-settings.js 2.4 kB -3 B (0%)
build/wc-shared-hocs.js 1.69 kB -1 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/atomic-block-components/image-frontend.js 1.77 kB 0 B
build/atomic-block-components/sale-badge.js 866 B 0 B
build/atomic-block-components/sku-frontend.js 389 B 0 B
build/atomic-block-components/sku.js 393 B 0 B
build/atomic-block-components/stock-indicator-frontend.js 569 B 0 B
build/atomic-block-components/stock-indicator.js 573 B 0 B
build/atomic-block-components/tag-list.js 473 B 0 B
build/editor-rtl.css 14.9 kB 0 B
build/editor.css 14.9 kB 0 B
build/vendors--atomic-block-components/price-frontend.js 6.53 kB 0 B
build/vendors-style-rtl.css 1.05 kB 0 B
build/vendors-style.css 1.05 kB 0 B
build/wc-payment-method-bacs.js 820 B 0 B
build/wc-payment-method-cod.js 913 B 0 B
build/wc-payment-method-paypal.js 853 B 0 B
build/wc-shared-context.js 1.53 kB 0 B

compressed-size-action

…thub.com:woocommerce/woocommerce-gutenberg-products-block into fix/3748-dont-load-wc_blocks_data-on-every-route
This was used for wc-admin <= v0.19.0 which is 1.5year old at this moment.
@budzanowski budzanowski changed the title Fix/3748 dont load wc blocks data on every route Dont load wc blocks data on every route Feb 19, 2021
@budzanowski
Copy link
Contributor Author

I have intended to rebase but ended up with merge. Fingers automation 😞

@budzanowski budzanowski marked this pull request as ready for review February 19, 2021 13:15
@budzanowski budzanowski requested a review from a team as a code owner February 19, 2021 13:15
@budzanowski budzanowski requested review from senadir and removed request for a team February 19, 2021 13:15
@budzanowski budzanowski changed the title Dont load wc blocks data on every route Don't load wc blocks data on every route. Feb 19, 2021
'shippingMethodsExist',
false
);
const blockConstants = getSetting( 'block_constants', {} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, I know we're not using this publicly yet but it should be highlighted at least.
For example, I used getSetting( 'displayCartPricesIncludingTax', false ); which is not longer available.
What's the reason for moving all data under block_constants?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the refactor each block tells the assets registry that it needs the constants. Until a block rendered happens the AssetsRegistery will not trigger the constants creation. This was the sole purpose of the refactor - no to trigger the wc_data_creation on every route. And now with that in mind the reason for the block_constant: since this is no longer global and each block does it is own registering we need a way to easily check if the data was already requested - so we will be able to avoid unnecessary multiple registers. This is managed by the AbstractBlock:

https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3864/files#diff-d1b778e863fef808d0b34565afe0c8bc172cc3d9e3e99324b402a19577f1fbf1R333-R336

// Enqueue common data.
if ( ! $this->asset_data_registry->exists( 'block_constants' ) ) {
	$this->asset_data_registry->add( 'block_constants', [ self::class, 'get_wc_block_data' ] );
}

We have only one key that we need to check. Any other dynamic approach way would require adjusting the AssetsDataRegistery framework for this special case of blocks common data which I think we should not do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Nadir here, this is a pretty significant breaking change and should be reconsidered. I'm not sure I agree with the solution in this refactor. I agree there needs to be a way for each block to register what data it needs (without triggering the error from duplicate keys) but I would prefer to see that happen at the block level. To avoid having to check if the data is available before registering (via AssetsDataRegistry::exists) - we could add a new helper method addIfNotExists or something like that which absorbs the necessary exists check and avoids registering duplicate data on the same key.

Then blocks that share data could potentially do the registration via a method on the abstract block class.

Some variation of the above should work and allows us to preserve back compat.

Copy link
Contributor

@nerrad nerrad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@budzanowski I realize you won't be following up on this given your rotation, but just thought I'd leave my review here. I might pick this up this week so we can get it merged. Thanks for the work you did on this!

import { getSetting } from '@woocommerce/settings';
import { WORD_COUNT_TYPE } from '@woocommerce/block-settings';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's switch back to use getSetting here. I'd like us to move away from using block-settings and use getSetting instead. That helps make the dependency much clearer.

This comment applies to all the other places modified to use getSetting.

'shippingMethodsExist',
false
);
const blockConstants = getSetting( 'block_constants', {} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Nadir here, this is a pretty significant breaking change and should be reconsidered. I'm not sure I agree with the solution in this refactor. I agree there needs to be a way for each block to register what data it needs (without triggering the error from duplicate keys) but I would prefer to see that happen at the block level. To avoid having to check if the data is available before registering (via AssetsDataRegistry::exists) - we could add a new helper method addIfNotExists or something like that which absorbs the necessary exists check and avoids registering duplicate data on the same key.

Then blocks that share data could potentially do the registration via a method on the abstract block class.

Some variation of the above should work and allows us to preserve back compat.

Comment on lines +130 to +135
$message = __(
'The filter should not be used for assets registration. Please read woocommerce-gutenberg-products-block/docs/contributors/block-assets.md for more information on how to proceed.',
'woo-gutenberg-products-block'
);

$settings = apply_filters_deprecated( 'woocommerce_shared_settings', [ $this->data ], '4.6.0', '', $message );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

$asset_api = $container->get( AssetApi::class );
$load_back_compat = defined( 'WC_ADMIN_VERSION_NUMBER' )
&& version_compare( WC_ADMIN_VERSION_NUMBER, '0.19.0', '<=' );
return $load_back_compat
? new BackCompatAssetDataRegistry( $asset_api )
: new AssetDataRegistry( $asset_api );
$asset_api = $container->get( AssetApi::class );
return new AssetDataRegistry( $asset_api );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see this removed 👏

@mikejolley mikejolley self-assigned this Apr 12, 2021
@mikejolley mikejolley closed this Apr 22, 2021
@ralucaStan ralucaStan deleted the fix/3748-dont-load-wc_blocks_data-on-every-route branch August 4, 2021 13:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't load wc_blocks_data on every route unnecessarily.
4 participants