-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Blocks: Include pre-filter settings in block type registration deprecation filter applications. #16518
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
Blocks: Include pre-filter settings in block type registration deprecation filter applications. #16518
Conversation
aduth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably update the corresponding test case to assert on the updated behavior.
gutenberg/packages/blocks/src/api/test/registration.js
Lines 341 to 375 in 5756b29
| it( 'should apply the blocks.registerBlockType filter to each of the deprecated settings as well as the main block settings', () => { | |
| const blockSettingsWithDeprecations = { | |
| ...defaultBlockSettings, | |
| deprecated: [ | |
| { | |
| save() { | |
| return 1; | |
| }, | |
| }, | |
| { | |
| save() { | |
| return 2; | |
| }, | |
| }, | |
| ], | |
| }; | |
| addFilter( 'blocks.registerBlockType', 'core/blocks/without-title', ( settings ) => { | |
| return { | |
| ...settings, | |
| attributes: { | |
| ...settings.attributes, | |
| id: { | |
| type: 'string', | |
| }, | |
| }, | |
| }; | |
| } ); | |
| const block = registerBlockType( 'my-plugin/fancy-block-13', blockSettingsWithDeprecations ); | |
| expect( block.attributes.id ).toEqual( { type: 'string' } ); | |
| expect( block.deprecated[ 0 ].attributes.id ).toEqual( { type: 'string' } ); | |
| expect( block.deprecated[ 1 ].attributes.id ).toEqual( { type: 'string' } ); | |
| } ); |
packages/blocks/src/api/constants.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should include a JSDoc describing the purpose of this value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it!
|
Related (at least partly fixes): #16502 |
|
Maybe relevant to update here, another consequence of #16348. We previously assumed it was okay (or at least expected) to mutate settings passed through these filters: gutenberg/packages/block-editor/src/hooks/custom-class-name.js Lines 34 to 41 in 8995efd
I expect might be problematic for multiple runs over the same settings, though I guess if we're merging the objects (creating a new reference) before calling the filter on the deprecated entries, we might not have an issue? Wondering if it would still be safer to create a shallow clone within our own implementation, rather than mutate directly (or, conversely, our own implementation would help us assure mutations work as expected). Observed as an error when trying to load the editor in Firefox (in Gutenberg 6.1, only when Jetpack activated): |
Yeah this wasn't a problem during calls for deprecations, but the main call could still mutate it and pass down mutations to the subsequent calls for deprecations. It's fixed now.
I think we should avoid mutations going forward, but they should not cause any issues and we can't guarantee plugins won't do it.
I don't think that's because of this PR. |
aduth
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was never able to reproduce the original issue, but the changes seem sensible and I encounter no issues with block registration when running the branch. I might also suggest a look from @talldan to confirm, ideally targeting a patch release tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Between this and the shallow clone above, there's some added overhead in the registration of each block. I don't think it will have a substantial impact, but noting since it impacts page load. We could potentially optimize this to skip the Array#map (and also the preFilterSettings assignment above) if ! hasFilter( 'blocks.registerBlockType' ).
That said, now that I write this, I realize since the editor has some default filtering, it will always be true. Perhaps in the future if we refactor away from those filters, we could get some wins here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps in the future if we refactor away from those filters, we could get some wins here.
That would be nice and would help in other places too probably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could maybe do for an inline comment explaining the purpose of the merge, omit, pick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it!
No, in fact, I'd venture to guess the changes here would resolve the error. |
|
|
The tests appear to be failing. |
…ation filter applications.
…r to avoid mutations in subsequent calls for deprecations.
0ae97ab to
57e4623
Compare
…ult block settings.
talldan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for spotting and fixing the error @epiqueras 🙇
I was also unable to reproduce the original issue, so I haven't been able to validate the fix. Any reproduction steps for the issue?
Was the core of the issue that Jetpack's withVideoPressEdit function had an undefined CoreVideoEdit argument?
https://github.com/Automattic/jetpack/blob/4c63a49fc0139c3296248e09ee69dedb537e024f/extensions/blocks/videopress/edit.js#L20
That does make me wonder whether error handling might also be valuable with this filter (and probably other parts of the api). Even if it's just to throw a slightly more informative error. Worth getting this fix in first though, and then that can be debated.
|
You need to run Gutenberg 6.1 with the latest Jetpack Blocks changes and I don't think the version in the plugin directory has them.
The
Yeah, we could wrap the main filter application and each deprecation application with their own try/catch. @youknowriad What do you think? |
Extensibility APIs are in general good candidate for error boundaries/handling. |
|
True, an entire plugin could break if one of their filters was not applied. |
|
I managed to reproduce this with latest Jetpack master and Gutenberg 6.1.0, and verified that this PR resolves the issue. |
…ation filter applications. (#16518) * Blocks: Include pre-filter settings in block type registration deprecation filter applications. * Blocks: Test new deprecation filter logic. * Blocks: Add JSDoc to new deprecation entry keys constant. * Blocks: Shallow copy settings passed to block type registration filter to avoid mutations in subsequent calls for deprecations. * Blocks: Add inline comments for new deprecation filter logic. * Blocks: Update tests for new deprecation filter logic to use new default block settings.
* Blocks: Include pre-filter settings in block type registration deprecation filter applications. (#16518) * Blocks: Include pre-filter settings in block type registration deprecation filter applications. * Blocks: Test new deprecation filter logic. * Blocks: Add JSDoc to new deprecation entry keys constant. * Blocks: Shallow copy settings passed to block type registration filter to avoid mutations in subsequent calls for deprecations. * Blocks: Add inline comments for new deprecation filter logic. * Blocks: Update tests for new deprecation filter logic to use new default block settings. * Edit Widgets: Don't run Customizer setup on every preview refresh. (#16544)

Fixes https://meta.trac.wordpress.org/ticket/4597
Description
PR #16348 created errors for plugins that have block type registration filters in which they expect properties not present in a deprecation entry.
A specific example of this is:
https://github.com/Automattic/jetpack/blob/4c63a49fc0139c3296248e09ee69dedb537e024f/extensions/blocks/videopress/editor
Which expects
settings.editto exist and throws an error that hangs the editor on a white screen.How has this been tested?
Tests for the new functionality were added in the block type registration tests.
Test suites were ran and they all passed.
Types of Changes
Bug Fix: Include pre-filter settings in block type registration deprecation filter applications to avoid uncaught errors.
Checklist: