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

WordPress 5.0 beta 1 - Javascript error for enqueued custom blocks #11069

Closed
bfintal opened this issue Oct 25, 2018 · 10 comments · May be fixed by monzerfoda/gutenberg#20
Closed

WordPress 5.0 beta 1 - Javascript error for enqueued custom blocks #11069

bfintal opened this issue Oct 25, 2018 · 10 comments · May be fixed by monzerfoda/gutenberg#20
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@bfintal
Copy link
Contributor

bfintal commented Oct 25, 2018

Describe the bug
I have a custom block that uses objects from wp.components and wp.editor like RangeControl and InspectorControls. I'm getting errors in 5.0 beta 1 in the editor such as this:

The editor has encountered an unexpected error.

Uncaught TypeError: Cannot read property 'RangeControl' of undefined

Checking the console, it's clear that wp.components and wp.editor are both present. But it's like my custom blocks are ran first before the dependencies.

There's no error if the Gutenberg plugin 4.1 is used instead.

The fix for beta 1 that I tried is to include wp-components and wp-editor as dependencies upon enqueuing the custom blocks. Doing that seems to get rid of the error, but again, the error isn't present outside 5.0 beta 1

To Reproduce
Steps to reproduce the behavior:

  1. Install WordPress 5.0 beta 1
  2. Install & activate the Stackable plugin https://wordpress.org/plugins/stackable-ultimate-gutenberg-blocks/
  3. Edit a post

Expected behavior
No errors

Desktop (please complete the following information):

  • OS: MacOS High Sierra 10.13.6
  • Browser: Chrome
  • Version: 69.0.3497.100
@dimensionmedia
Copy link

@bfintal Experiencing the same issue with several custom blocks i've created. Can you go into detail on how you includes them as "dependencies upon enqueuing the custom blocks"?

@dennyf
Copy link

dennyf commented Oct 29, 2018

@dimensionmedia When you enqueue the JS script that registers the custom blocks, just add wp-components and wp-editor to the wp_enqueue_script function's dependency parameter:

wp_enqueue_script(
    'my-custom-block-js',
    plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ),
    array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ),
    true
);

@dimensionmedia
Copy link

@dennyf thank you. it's been so long since i've seen that part of my code, I was drawing a blank. I confirmed that I'm no longer getting the JS errors and assume for now this is an accepted solution but will continue to monitor.

@bfintal
Copy link
Contributor Author

bfintal commented Oct 30, 2018

Similarly, styles which are enqueued via wp_enqueue_style with a dependency for wp-blocks now do not work. The fix is to remove all dependencies for it.

wp_enqueue_style(
	'my-custom-style-css',
	plugins_url( 'dist/blocks.style.build.css', dirname( __FILE__ ) ),
	array()
);

benlk added a commit to Automattic/super-cool-ad-inserter-plugin that referenced this issue Oct 30, 2018
@benlk
Copy link
Contributor

benlk commented Oct 30, 2018

Confirming that this also happens in WordPress 5.0-beta2-43845, and that (in my plugin's case) adding wp-components and wp-editor to the enqueue as dependencies fixes the problem: Automattic/super-cool-ad-inserter-plugin@1c9202c

@designsimply designsimply added the Needs Technical Feedback Needs testing from a developer perspective. label Nov 5, 2018
@designsimply
Copy link
Member

Noting that bugs that happen in WordPress 5.0 and that do not happen for the Gutenberg plugin should be reported in https://core.trac.wordpress.org/

I wanted to help test before moving the issue but I was unable to reproduce the problem and wanted to check in with you.

First, I tested by installing and activating Stackable – Ultimate Gutenberg Blocks 1.4 on WordPress 5.0-beta3-43865 and opening a post for editing and I did not see any errors. I did noticed that the 1.4 changelog mentions fixing invalid script and style dependencies though.

Next, I installed and activated Super Cool Ad Inserter Plugin 0.2 on WordPress 5.0-beta3-43865 and manually created an add as a widget and I was able to see the ad display on a test post and did not see any errors in the browser console.

@bfintal @benlk since it appears you were able to address the problems, can this issue be closed?

@benlk
Copy link
Contributor

benlk commented Nov 5, 2018

@designsimply I believe so; this issue is caused by plugin developers not declaring dependencies where Gutenberg-as-plugin had enqueued those dependencies by default.

@designsimply
Copy link
Member

Cheers @benlk and thanks for the quick reply!

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. and removed Needs Technical Feedback Needs testing from a developer perspective. labels Nov 6, 2018
@designsimply
Copy link
Member

Cross-referencing another similar report from WP 5.0 in case it's helpful to someone else later! https://core.trac.wordpress.org/ticket/45219

@bfintal
Copy link
Contributor Author

bfintal commented Nov 6, 2018

@designsimply @benlk Thanks! Yup I've addressed the problem. However, I think this might mean that the handbook should have more notes regarding dependencies https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type/

jghazally added a commit to jghazally/gutenberg that referenced this issue Nov 26, 2018
Previously Gutenberg Plugin added these dependencies, related to:
https://core.trac.wordpress.org/ticket/45219
and
WordPress#11069
mkaz added a commit that referenced this issue Nov 27, 2018
Issue #11069 highlighted that the required dependencies
should be called out more in the documentation. This PR
adds a line adding additional information on including.
gziolo pushed a commit that referenced this issue Nov 27, 2018
Issue #11069 highlighted that the required dependencies
should be called out more in the documentation. This PR
adds a line adding additional information on including.
taija added a commit to BellevueCollege/mayflower-blocks that referenced this issue Dec 17, 2018
Block script loaded before WP core scripts, preventing functionality.
See WordPress/gutenberg#11069
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants