Only register block assets on the frontend during block render (#51517)
* Only register block assets on the frontend during block render * Add changelog entry * Add wc-blocks-registry as a dependency to product collection tester script * Update docs manifest
This commit is contained in:
parent
9647db4bb6
commit
10b85cefb8
|
@ -1050,7 +1050,7 @@
|
|||
"menu_title": "Registering custom collections",
|
||||
"tags": "how-to",
|
||||
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/docs/product-collection-block/register-product-collection.md",
|
||||
"hash": "6d32bc27924226b032e03624dbeedde3c899c2e8eb777a1fece93bed99544f03",
|
||||
"hash": "021a567cece90d4c56991d0778d7f291d1489c7a559b6fade5f001cd9920dc77",
|
||||
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/docs/product-collection-block/register-product-collection.md",
|
||||
"id": "3bf26fc7c56ae6e6a56e1171f750f5204fcfcece"
|
||||
},
|
||||
|
|
|
@ -27,6 +27,20 @@ The `__experimentalRegisterProductCollection` function is part of the `@woocomme
|
|||
});
|
||||
```
|
||||
|
||||
Be sure to add `wc-blocks-registry` as a dependency to your script if you opt to use the `wc` global.
|
||||
|
||||
```php
|
||||
function enqueue_my_custom_product_collection_script() {
|
||||
wp_enqueue_script(
|
||||
'my-custom-product-collection',
|
||||
plugins_url( '/dist/my-custom-product-collection.js', __FILE__ ),
|
||||
array( 'wc-blocks-registry' ),
|
||||
10
|
||||
);
|
||||
}
|
||||
add_action( 'enqueue_block_editor_assets', 'enqueue_my_custom_product_collection_script' );
|
||||
```
|
||||
|
||||
> [!TIP]
|
||||
> The first method is recommended if you are using Webpack.
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ function register_product_collections_script()
|
|||
wp_enqueue_script(
|
||||
'rpc_register_product_collections',
|
||||
plugins_url('register-product-collection-tester/index.js', __FILE__),
|
||||
array('wp-element', 'wp-blocks', 'wp-i18n', 'wp-components', 'wp-editor', 'wc-blocks'),
|
||||
array('wp-element', 'wp-blocks', 'wp-i18n', 'wp-components', 'wp-editor', 'wc-blocks', 'wc-blocks-registry'),
|
||||
filemtime(plugin_dir_path(__FILE__) . 'register-product-collection-tester/index.js'),
|
||||
true
|
||||
);
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: performance
|
||||
|
||||
Limit block asset registration to rendered blocks on the frontend
|
|
@ -90,9 +90,9 @@ abstract class AbstractBlock {
|
|||
* @return string Rendered block type output.
|
||||
*/
|
||||
public function render_callback( $attributes = [], $content = '', $block = null ) {
|
||||
|
||||
$render_callback_attributes = $this->parse_render_callback_attributes( $attributes );
|
||||
if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
|
||||
$this->register_block_type_assets();
|
||||
$this->enqueue_assets( $render_callback_attributes, $content, $block );
|
||||
}
|
||||
return $this->render( $render_callback_attributes, $content, $block );
|
||||
|
@ -107,6 +107,7 @@ abstract class AbstractBlock {
|
|||
if ( $this->enqueued_assets ) {
|
||||
return;
|
||||
}
|
||||
$this->register_block_type_assets();
|
||||
$this->enqueue_data();
|
||||
}
|
||||
|
||||
|
@ -122,7 +123,6 @@ abstract class AbstractBlock {
|
|||
return false;
|
||||
}
|
||||
$this->integration_registry->initialize( $this->block_name . '_block' );
|
||||
$this->register_block_type_assets();
|
||||
$this->register_block_type();
|
||||
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue