Skip to content
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

InnerBlocks: Make sure blockType is set before trying to use it #63351

Merged

Conversation

mattsherman
Copy link
Contributor

What?

This PR adds a check that blockType is set before attempting to use it in InnerBlocks.

Why?

This PR addresses an error that is thrown in the context of the WooCommerce new product editor when generating product variations. See woocommerce/woocommerce#49216 for a try branch that triggers the error.

Without this fix, whenever variations are generated, the following error is logged in the browser dev console (note, we worked around the error in the UI in woocommerce/woocommerce#49248, so it does not crash WooCommerce).

TypeError: undefined is not an object (evaluating 'w.providesContext')

How?

This PR adds a check that blockType is set before attempting to use it in InnerBlocks.

Testing Instructions

With the Gutenberg plugin built from this PR and a WooCommerce env running the branch from woocommerce/woocommerce#49216 with the new product editor enabled (WooCommerce > Settings > Advanced > Features > Experimental features)...

  1. Go to Products > Add New
  2. On the Variations tab, click Add Options and add some variation options.
  3. Verify that the variations are added correctly and the selected tab remains Variations
  4. Verify that no TypeError: undefined is not an object (evaluating 'w.providesContext') error was logged in the browser dev console.

Testing Instructions for Keyboard

n/a

Screenshots or screencast

n/a

Copy link

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: mattsherman <mattormeeple@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

if ( Object.keys( blockType.providesContext ).length === 0 ) {
if (
! blockType?.providesContext ||
Object.keys( blockType.providesContext ).length === 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not fully understanding how it's possible to have inner blocks without a block type. Could you give an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I am not sure how this is happening either! But, it happens in the scenario mentioned in the description with WooCommerce's use of blocks in the new product editor. Ultimately, the root problem may lie somewhere in WooCommerce's handling of blocks, but this change seems like a reasonable defensive change in GB.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm still curious. This is a legitimate error tbh, but sure, let's gracefully fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We will still be attempting to address the root cause of this! I'm very curious too.

@ellatrix ellatrix added the [Type] Bug An existing feature does not function as intended label Jul 10, 2024
@ellatrix ellatrix merged commit 6b53293 into WordPress:trunk Jul 10, 2024
66 of 68 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.9 milestone Jul 10, 2024
carstingaxion pushed a commit to carstingaxion/gutenberg that referenced this pull request Jul 18, 2024
Co-authored-by: mattsherman <mattormeeple@git.wordpress.org>
@priethor priethor added the [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P [Type] Bug An existing feature does not function as intended
3 participants