-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fail gracefully when block in createBlock
function is not registered
#68043
base: trunk
Are you sure you want to change the base?
Fail gracefully when block in createBlock
function is not registered
#68043
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
createBlock
function
createBlock
functioncreateBlock
function is not registered
This is a good direction which follows established handling. Example: gutenberg/packages/blocks/src/api/factory.js Lines 595 to 601 in 09eb60e
gutenberg/packages/blocks/src/api/templates.js Lines 118 to 133 in 09eb60e
Now, that |
…ases they rely on internal handling of `createBlock` function.
@gziolo, good suggestion! I made the requested changes in b036d44. Let me know if that's what you had in mind. I see there's one job failing but seems like it's some env issue but I can't rerun the job. |
What?
In this PR I'm checking if block is registered before sanitizing its attributes. If it doesn't exist, return
core/missing
to gracefully handle the situation.Why?
This is to preserve editor or inserter crash if
createBlock
orcreateBlocksFromInnerBlocksTemplate
uses a block that is unregistered for any reason (e.g. due some custom logic in plugins).Example scenario:
createBlocksFromInnerBlocksTemplate
and depends oncore/post-title
core/post-title
is unregistered (for example other plugin provides "updated" version of Post Title block)scope: [ 'inserter' ]
) it's enough it will appear in results to cause a crash. It will be "rendered" for preview purpose.To prevent both cases, the
core/missing
block should be rendered, rather than throwing error, to gracefully handle this situation.Note
I'm well aware this scenario is against Gutenberg's principles and there should not be the case in which core blocks are unregistered. But at the same time it may happen and
unregisterBlockType
function is there so any plugin can use it. This PR's purpose is not to encourage that but to gracefully fail instead of crashing editor which prevents further usage of blocks.How?
Verify if block is registered and return
core/missing
if not.Testing Instructions
core/post-title
by pastingwp.blocks.unregisterBlockType('core/post-title')
(this is to imitate some other plugin does that)core/missing
is rendered instead of Post Title mentioning its name.Testing Instructions for Keyboard
Screenshots or screencast