Prevent duplicate registration of core blocks in client (#37350)

* Prevent duplicate registration of core blocks

* Add components changelog entry
This commit is contained in:
Joshua T Flowers 2023-03-22 16:56:35 -07:00 committed by GitHub
parent 5a21b3f218
commit 562749b60a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Prevent duplicate registration of core blocks in client

View File

@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { BlockInstance } from '@wordpress/blocks';
import { BlockInstance, getBlockType } from '@wordpress/blocks';
import {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We need this to import the block modules for registration.
@ -29,9 +29,10 @@ const ALLOWED_CORE_BLOCKS = [
const registerCoreBlocks = () => {
const coreBlocks = __experimentalGetCoreBlocks();
const blocks = coreBlocks.filter( ( block: BlockInstance ) =>
ALLOWED_CORE_BLOCKS.includes( block.name )
);
const blocks = coreBlocks.filter( ( block: BlockInstance ) => {
const isRegistered = !! getBlockType( block.name );
return ! isRegistered && ALLOWED_CORE_BLOCKS.includes( block.name );
} );
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore An argument is allowed to specify which blocks to register.

View File

@ -0,0 +1,4 @@
Significance: minor
Type: fix
Prevent duplicate registration of core blocks in client

View File

@ -1,7 +1,13 @@
/**
* External dependencies
*/
import { registerCoreBlocks } from '@wordpress/block-library';
import { BlockInstance, getBlockType } from '@wordpress/blocks';
import {
registerCoreBlocks,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We need this to import the block modules for registration.
__experimentalGetCoreBlocks,
} from '@wordpress/block-library';
/**
* Internal dependencies
@ -14,7 +20,14 @@ import { init as initPricing } from '../pricing-block';
import { init as initCollapsible } from '../collapsible-block';
export const initBlocks = () => {
registerCoreBlocks();
const coreBlocks = __experimentalGetCoreBlocks();
const blocks = coreBlocks.filter( ( block: BlockInstance ) => {
return ! getBlockType( block.name );
} );
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore An argument is allowed to specify which blocks to register.
registerCoreBlocks( blocks );
initName();
initSummary();
initSection();