Skip to content

Commit

Permalink
Block Directory: Bootstrap all blocks metadata to avoid missing inner…
Browse files Browse the repository at this point in the history
…blocks of newly installed Block
  • Loading branch information
karthikeya-io committed Mar 5, 2025
1 parent 4e16551 commit 617735d
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/block-directory/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const installBlockType =

// Ensures that the block metadata is propagated to the editor when registered on the server.
const metadataFields = [
'name',
'api_version',
'title',
'category',
Expand All @@ -103,23 +104,32 @@ export const installBlockType =
'variations',
];
await apiFetch( {
path: addQueryArgs( `/wp/v2/block-types/${ name }`, {
path: addQueryArgs( `/wp/v2/block-types/`, {
_fields: metadataFields,
} ),
} )
// Ignore when the block is not registered on the server.
.catch( () => {} )
.then( ( response ) => {
if ( ! response ) {
if ( ! response && ! Array.isArray( response ) ) {
return;
}
unstable__bootstrapServerSideBlockDefinitions( {
[ name ]: Object.fromEntries(
Object.entries( response ).filter( ( [ key ] ) =>
metadataFields.includes( key )
)
),
} );
const blockDefinitions = Object.fromEntries(
response.map( ( blockItem ) => [
blockItem.name,
Object.fromEntries(
Object.entries( blockItem ).filter(
( [ key ] ) =>
metadataFields.includes( key )
)
),
] )
);

// Bootstrap all retrieved block definitions
unstable__bootstrapServerSideBlockDefinitions(
blockDefinitions
);
} );

await loadAssets();
Expand Down

0 comments on commit 617735d

Please sign in to comment.