Skip to content

Gutenpack: Only load frontend assets as required - #10405

Merged
enejb merged 13 commits into
masterfrom
update/jetpack-gutenber-only-load-assets-as-required
Oct 30, 2018
Merged

enejb merged 13 commits into
masterfrom
update/jetpack-gutenber-only-load-assets-as-required

Conversation

@enejb

@enejb enejb commented Oct 25, 2018 •

Copy link
Copy Markdown
Member

Fixes #10325

Requires you to build the blocks via this branch:
Automattic/wp-calypso#28066

This PR implement a new jetpack_register_block() function. Which helps Jetpack have a better understand what blocks are supported and active. Different modules could be disabled. This information could then also be used in the editor to show/hide the blocks there as well.

I only added the tiled-gallery block because for now it is the only block that has frontend assets.

Testing instructions:

URL: JN test

Create a new tile gallery block.
Notice that the tile gallery view.js is only loaded on the page where the block appears.

Proposed changelog entry for your changes:

Only load front end assets as required by the block.

@enejb
enejb requested a review from a team October 25, 2018 21:39
@jetpackbot

jetpackbot commented Oct 25, 2018 •

Copy link
Copy Markdown
Collaborator

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: November 6, 2018.
Scheduled code freeze: October 30, 2018

Generated by 🚫 dangerJS

@enejb enejb self-assigned this Oct 25, 2018
@enejb enejb added the [Status] Needs Review This PR is ready for review. label Oct 25, 2018
@enejb
enejb requested review from a team, jeffersonrabb, simison and sirreal October 25, 2018 21:55
@enejb enejb added the [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack label Oct 25, 2018
@jeherve jeherve added this to the 6.7 milestone Oct 26, 2018
@jeherve jeherve added the [Feature] Tiled Gallery A different way to display image galleries on your site, in different organizations and shapes. label Oct 26, 2018

@jeherve jeherve left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't seem to see any tiled gallery on the front end of my site when inserting a gallery via the Gutenberg block. I consequently do not see the gallery assets in the source code either. Maybe that's partly because #10375 has not been merged yet?

I also left a few comments about minor coding standards changes you could make here, so our code editors don't shout at us when we open the file :)

There also seems to be a small issue with how the file is built for RTL sites.

Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack.php Resolved
Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack-gutenberg.php Resolved
Comment thread modules/tiled-gallery.php Resolved
@jeherve jeherve added [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. and removed [Status] Needs Review This PR is ready for review. labels Oct 26, 2018
@sirreal sirreal mentioned this pull request Oct 26, 2018
2 of 4 tasks
@lezama

lezama commented Oct 26, 2018

Copy link
Copy Markdown
Contributor

@jeherve thanks for the review!
@enejb I applied linting and added a missing ! now we are enqueuing the assets 🎉

@jeherve

jeherve commented Oct 26, 2018

Copy link
Copy Markdown
Member

@lezama Thanks! We still have that RTL issue though:
#10405 (comment)

And the missing docblocks. :)

@enejb

enejb commented Oct 26, 2018

Copy link
Copy Markdown
Member Author

@jeherve Can you elaborate on the RTL issue? I didn't find any issues but I also wasn't able to test it properly since the files are not there.

@enejb

enejb commented Oct 26, 2018

Copy link
Copy Markdown
Member Author

Nevermind good catch on the RTL issue. 🤦‍♂️

@jeffersonrabb

Copy link
Copy Markdown
Contributor

When testing locally (JP docker) I'm getting This block contains unexpected or invalid content. for the Tiled Gallery after saving, and nothing displays in the frontend after publishing the post. The JN link is also failing. However, I do see the tiled-gallery's view.js file loading in the frontend, and I don't see it if the block is removed, so that's looking good.

@enejb

enejb commented Oct 26, 2018

Copy link
Copy Markdown
Member Author

Thanks for testing the PR @jeffersonrabb. I also am running into the same issue. With the tile gallery.
Maybe the tile gallery is not ready yet? Or it needs more changes?
@tyxla and @simison would know more.

@simison

simison commented Oct 26, 2018

Copy link
Copy Markdown
Member

The tiled gallery currently in master isn't very stable indeed (and the new one isn't merged yet Automattic/wp-calypso#27458).

It might make sense to update our testing block hello-dolly to contain some styles and use that for testing... you could probably re-use this PR: Automattic/wp-calypso#27241

@jeherve jeherve mentioned this pull request Oct 29, 2018
18 of 22 tasks
Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack-gutenberg.php Resolved
return (bool) apply_filters( 'jetpack_gutenberg', true );
}

public static function load_assets_as_required( $type ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm thinking we could use a filter inside this function, allowing us (or a third party) to disable loading of these in the last minute. It would be useful if someone needs a more granular control over what is to be loaded on a per block basis (contrary to jetpack_gutenberg, which applies to all).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

True. But do we want those filters right now? I think they can be done in a different PR when they are requested. There is also the previous way of dequeuing the assets. Which is done just before enqueuing them.

* @param bool false Whether to load Gutenberg blocks from CDN
* @param bool true Whether to load Gutenberg blocks
*/
if ( apply_filters( 'jetpack_gutenberg_cdn', false ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We seem to completely be removing loading of the assets from CDN option. While we don't use it much anymore, I don't see any mention about it in this PR. It would be nice to make sure we remove it properly and we don't rely on it being here anywhere - sounds like material for another PR to me.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No there is still the CDN option but only for the editor assets. I didn't want to remove it since it was already taken care of by a different PR.

Comment thread class.jetpack-gutenberg.php Resolved
Comment thread class.jetpack.php
* Prepare Gutenberg Editor functionality
*/
require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
add_action( 'enqueue_block_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_assets' ) );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like we'll no longer use core Gutenberg's hook enqueue_block_assets to enqueue the assets. Core Gutenberg recommends using that hook, because it assures that its necessary assets will already be loaded. Do we want to use it after all, even if we use init for our business logic?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we are fine with not using it. since the current enqueue_block_assets hood also fires on the front end and results in assets being loaded when they don't have to be.

Comment thread modules/tiled-gallery.php Resolved
Comment thread modules/tiled-gallery.php Resolved
Comment thread modules/tiled-gallery.php Resolved
@jeherve

jeherve commented Oct 30, 2018

Copy link
Copy Markdown
Member

Rebased and updated to account for #10405 (comment)

It's hard to test with just Tiled Galleries, though.

enejb and others added 12 commits October 30, 2018 10:48
This Removed the loading of the front end code that comes as a bundle of all the files.
And make sure that it loads only the front end code that is needed when the block is going to be displayed.
This lets us register the tiled gallery code to be loaded on the front end only
We'll rely on those functions a lot in the next few months, the different comments should help us get all
the info we need in our IDEs.
It also acts as a good example for other folks who may be interested in how Jetpack adds support for its
blocks.
@enejb
enejb force-pushed the update/jetpack-gutenber-only-load-assets-as-required branch from 54940f3 to f8098a2 Compare October 30, 2018 18:06
@enejb enejb added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Oct 30, 2018

@oskosk oskosk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@oskosk oskosk added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Oct 30, 2018
@oskosk

oskosk commented Oct 30, 2018

Copy link
Copy Markdown
Contributor

Testing this PR on WordPress 5.0 Beta 2 (Gutenberg in core, not as a plugin) I get this error when attempting to include the Gallery block

react-dom.min.js?ver=5.0-beta2-43845:104 TypeError: Cannot read property 'length' of undefined

image

@enejb
enejb merged commit d7bd98b into master Oct 30, 2018
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Oct 30, 2018
@enejb
enejb deleted the update/jetpack-gutenber-only-load-assets-as-required branch October 30, 2018 22:08
@jeherve

jeherve commented Oct 31, 2018

Copy link
Copy Markdown
Member

Noting that #10491 reverted some of our work here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Tiled Gallery A different way to display image galleries on your site, in different organizations and shapes. [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants