From 656de501f8d5da1ce15d468bf544a1e261d6f8f3 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 18 Apr 2023 21:53:08 -0400 Subject: [PATCH 1/2] Fix infinite loop (range error) on subscribe callback. --- .../register-block-single-product-template.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/assets/js/atomic/utils/register-block-single-product-template.ts b/assets/js/atomic/utils/register-block-single-product-template.ts index 8b7e492f751..84427bfeb70 100644 --- a/assets/js/atomic/utils/register-block-single-product-template.ts +++ b/assets/js/atomic/utils/register-block-single-product-template.ts @@ -13,6 +13,9 @@ import { } from '@wordpress/blocks'; import { subscribe, select } from '@wordpress/data'; +// Creating a local cache to prevent multiple registration tries. +const blocksRegistered = new Set(); + export const registerBlockSingleProductTemplate = ( { blockName, blockMetadata, @@ -86,16 +89,21 @@ export const registerBlockSingleProductTemplate = ( { }, 'core/edit-site' ); subscribe( () => { - const isBlockRegistered = Boolean( getBlockType( blockName ) ); - const editPostStoreExists = Boolean( select( 'core/edit-post' ) ); - - if ( ! isBlockRegistered && editPostStoreExists ) { + const isBlockRegistered = Boolean( variationName ) + ? blocksRegistered.has( variationName ) + : blocksRegistered.has( blockName ); + // This subscribe callback could be invoked with the core/blocks store + // which would cause infinite registration loops because of the `registerBlockType` call. + // This local cache helps prevent that. + if ( ! isBlockRegistered ) { if ( isVariationBlock ) { + blocksRegistered.add( variationName ); registerBlockVariation( blockName, blockSettings as BlockVariation< BlockAttributes > ); } else { + blocksRegistered.add( blockName ); // @ts-expect-error: `registerBlockType` is typed in WordPress core registerBlockType( blockMetadata, blockSettings ); } From 4a49199d18cffd63fcabe2105d45ac609b8e6bbd Mon Sep 17 00:00:00 2001 From: tjcafferkey Date: Wed, 19 Apr 2023 04:01:04 +0100 Subject: [PATCH 2/2] Replace getEditedPostContext with getEditedPostId to retrieve the templateId from the Site Editor for 6.1.1 compatibility --- .../utils/register-block-single-product-template.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/assets/js/atomic/utils/register-block-single-product-template.ts b/assets/js/atomic/utils/register-block-single-product-template.ts index 84427bfeb70..9d4df9ba2e4 100644 --- a/assets/js/atomic/utils/register-block-single-product-template.ts +++ b/assets/js/atomic/utils/register-block-single-product-template.ts @@ -16,6 +16,10 @@ import { subscribe, select } from '@wordpress/data'; // Creating a local cache to prevent multiple registration tries. const blocksRegistered = new Set(); +function parseTemplateId( templateId: string | undefined ) { + return templateId?.split( '//' )[ 1 ]; +} + export const registerBlockSingleProductTemplate = ( { blockName, blockMetadata, @@ -34,9 +38,7 @@ export const registerBlockSingleProductTemplate = ( { subscribe( () => { const previousTemplateId = currentTemplateId; const store = select( 'core/edit-site' ); - currentTemplateId = store?.getEditedPostContext< { - templateSlug?: string; - } >()?.templateSlug; + currentTemplateId = parseTemplateId( store?.getEditedPostId() ); const hasChangedTemplate = previousTemplateId !== currentTemplateId; const hasTemplateId = Boolean( currentTemplateId );