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

[WIP] Enqueue only required frontend styles - V2 #6200

Closed
wants to merge 38 commits into from
Closed

[WIP] Enqueue only required frontend styles - V2 #6200

wants to merge 38 commits into from

Conversation

kanishkdudeja
Copy link

@kanishkdudeja kanishkdudeja commented Apr 16, 2018

This PR is a proof of concept for #5445. It only enqueues front-end styles for block types present in the current page/post.

Dependencies

Version 2 (enqueues styles inside <head>)

This branch reads the current post's content on the wp_enqueue_scripts action (much earlier than the_content). Therefore, it enables us to be able to enqueue required styles inside the <head> section.

An issue with this approach is that if a plugin adds a core block using a filter on the_content later, our code isn't able to enqueue styles for that block type since we already have read the post's content much earlier. Maybe we can try re-parsing the post's content to enqueue styles for any remaining block types after all filters on the_content have executed.

Version 1 (enqueues styles inside <body>)

#6166 hooks a filter on the_content to get block types present in the page while stripping block comments from the HTML.

Since filters hooked to the_content are executed much later in the lifecycle (after actions hooked to wp_enqueue_scripts are executed), the stylesheets get enqueued in the <body> section.

To Do

  • Make this work for dynamic blocks
  • Bundling for styles (Right now, styles for each block type are enqueued separately)
  • Provide a way to disable this functionality (meaning, enqueue all styles regardless of block types present in the page)
  • Provide a way to extend this functionality

Kanishk Dudeja added 29 commits April 8, 2018 21:27
Added a class "WP_Parsed_Block_Types_Registry" to manage the storage of block types parsed from the HTML (while stripping block comments)
Core block types parsed from the HTML may not contain 'core/'. Added a function gutenberg_prefix_core_namespace_if_not_found() to do the same.
This is work in progress and will be completely overhauled.

Just committing it so that I don't lose it.
…e enqueued

The new logic checks if the current requested web page is in the frontend. And if it's page or a post.

If the answer is yes to both, it only enqueues styles for blocks which are present in the post/page.
Renamed gutenberg_prefix_core_namespace_if_not_found() to gutenberg_normalize_block_type()
Removed '$match = Array()' since we don't need  to declare a variable for preg_match()
Fixed syntax to suit coding standards
…blocks

Our rendering algorithm for dynamic blocks was stripping block comments after rendering those dynamic blocks.

Disabled that functionality since block comments are needed to find out block types present in the page later.
get_last_error() function in Block Type Validator was referencing $errors as a local variable, whereas it should have referenced it as a class variable.

Have fixed it in this commit.
Removed _doing_it_wrong() notices from Block Type validator class since the same are being called in the caller class.
…ments and other changes

Renamed gutenberg_strip_block_comments to gutenberg_process_block_comments
Used gutenberg_normalize_block_type() in function which renders dynamic blocks
Improved documentation blocks for some functions
…n which enqueues scripts and style handles
Kanishk Dudeja added 9 commits April 16, 2018 17:21
Earlier, we were parsing block types from the HTML and saving them to the registry even for pages like category / archive pages (which contain multiple posts).

Have disabled this functionality for such pages now since the registry is only needed for intelligent enqueueing of assets (which is needed for single post / page types as of now).
@karmatosed karmatosed requested review from aduth and mtias May 10, 2018 21:04
@gziolo gziolo mentioned this pull request May 14, 2018
@aduth aduth added [Status] In Progress Tracking issues with work in progress [Status] Blocked Used to indicate that a current effort isn't able to move forward labels May 14, 2018
@aduth
Copy link
Member

aduth commented Sep 13, 2018

This pull request appears to have languished and will not be easily reconciled with master. Please feel free to reopen and rebase against the current master, or open a new pull request.

@aduth aduth closed this Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] Blocked Used to indicate that a current effort isn't able to move forward [Status] In Progress Tracking issues with work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants