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

Framework: Make sure block assets are always registered before wp-edit-post script #6448

Merged
merged 2 commits into from
Apr 30, 2018

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Apr 26, 2018

Description

Fixes #6443 and #5661.

Raised by @korobochkin:

You can register your new Guten block with wp.blocks.registerBlockType function call. But it won’t work after page reloading

Raised by @theleemon:

Everything seems to work as expected: at first glance the block is rendered correctly both on the editor and the frontend. But, if I save the post, then preview it and at the end I come back to edit it again, the custom block is automatically rendered in the editor as a classic editor block, although the HTML code beneath it shows that it's still a custom block. The console shows no errors.

In my tests it turned out that all blocks related assets are loaded too late when their are forced to be included in the footer. It works perfectly fine when they are included in the header. This patch moves those assets to be include before wp-edit-post which moves them in the flow to be always included first regardless where there are located.

How has this been tested?

Manually. I registered the plugin with block using the code snippet generated with WP-CLI as explained in https://wordpress.org/gutenberg/handbook/blocks/generate-blocks-with-wp-cli/. I also updated:

    wp_register_script(
        'movie-block-editor',
        plugins_url( $block_js, __FILE__ ),
        array(
            'wp-blocks',
            'wp-i18n',
            'wp-element',
        ),
-        filemtime( "$dir/$block_js" )
+        filemtime( "$dir/$block_js" ),
+        true
    );

to ensure it works also when the script is loaded in footer.

I added the test block and made sure it still loads properly when I refreshed the page.

Types of changes

Bug fix (non-breaking change which fixes an issue).

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. Backwards Compatibility Issues or PRs that impact backwards compatability Framework Issues related to broader framework topics, especially as it relates to javascript [Feature] Block API API that allows to express the block paradigm. [Type] Bug An existing feature does not function as intended and removed [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. Backwards Compatibility Issues or PRs that impact backwards compatability labels Apr 26, 2018
@gziolo gziolo self-assigned this Apr 26, 2018
@gziolo gziolo requested review from mcsf, pento, aduth and a team April 26, 2018 15:40
*
* @since 0.4.0
*/
do_action( 'enqueue_block_editor_assets' );
Copy link
Contributor

Choose a reason for hiding this comment

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

What if a plugin enqueue blocks but also register a plugin depending on wp-edit-post module?

My proposal here is to add a JavaScript actiion registerBlocks called on domReach (the WP package). This way no matter the script loading order, the blocks will always be registered at the right moment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. An alternative would be to put inline script which initializes the editor under their own name and make wp-edit-post its dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I had in mind is something like that:

wp.domReady( () => {
    wp.hooks.doAction( 'registerBlocks' );
    wp.editPost.initializeEditor();
} );

And ask plugin authors to move their registerBlockType calls too the registerBlocks action. I know it might be a bit late for that, so the current method should work as well, and we could warn if registerBlockType is called before the hook.

@gziolo gziolo added the [Status] In Progress Tracking issues with work in progress label Apr 27, 2018
@gziolo
Copy link
Member Author

gziolo commented Apr 27, 2018

471aedb - is not production ready :)

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@gziolo gziolo force-pushed the fix/block-assets-before-edit-post branch from 471aedb to 0ca49c9 Compare April 30, 2018 08:26
@gziolo
Copy link
Member Author

gziolo commented Apr 30, 2018

Tested with npm run build.

Master

screen shot 2018-04-30 at 11 58 44

Branch

branch

I couldn't notice any regressions.

@gziolo gziolo merged commit 4e766f1 into master Apr 30, 2018
@gziolo gziolo deleted the fix/block-assets-before-edit-post branch April 30, 2018 09:59
@gziolo gziolo added this to the 2.8 milestone Apr 30, 2018
@gziolo gziolo removed the [Status] In Progress Tracking issues with work in progress label Apr 30, 2018
target
);
return new Promise( ( resolve ) => {
domReady( () => {
Copy link
Member

Choose a reason for hiding this comment

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

Should it be up to the editor to decide the specific time it should be initialized? Or should the prerequisite of the initialization be considered by that which is initializing it (thinking outside the specific "WP + Plugins" context).

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question. you mean moving this to client-assets instead. Sounds like a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

Now that you asked it this way, it sounds reasonable 😃
I fully focused on fixing the issue with blocks loaded too late and I didn’t think about it.
We will have to make dom-ready its own module but it would happen sooner or later anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. Framework Issues related to broader framework topics, especially as it relates to javascript [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom block rendered as a classic editor block automatically
3 participants