Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#53616 closed defect (bug) (fixed)

Registered block styles should only be rendered when the block exists on a page

Reported by: aristath Owned by: gziolo
Priority: normal Milestone: 5.8.1
Component: Script Loader Version: 5.9
Severity: normal Keywords: has-patch fixed-major
Cc: Focuses: performance

Description

Came up in ​a comment on make.
In WordPress 5.8 we added the ability to only load styles for blocks when these blocks are rendered. However, these optimizations left out block-styles that get added using the ​register_block_style() function/API.

Registered block-styles can either be added using a handle, or by adding directly some CSS, and enqueueing these styles takes place inside the ​enqueue_block_styles_assets function.
If a handle is used and wp_should_load_separate_core_block_assets returns true, the stylesheet should be enqueued on block_render.
If inline-CSS is used and wp_should_load_separate_core_block_assets returns true, then we should check if there is a stylesheet for that block, and if there is then switch the handle inside the wp_add_inline_style call from wp-block-library to the block's stylesheet handle.

Change History (11)

This ticket was mentioned in ​PR #1482 on ​WordPress/wordpress-develop by ​aristath.


5 years ago
#1

  • Keywords has-patch added

This PR modifies the enqueue_block_styles_assets function, adding conditions for wp_should_load_separate_core_block_assets() and making sure that registered block-styles only load when a block gets rendered on a page.

Trac ticket: https://core.trac.wordpress.org/ticket/53616

#2 @gziolo
5 years ago

Thank you for a quick fix. It will have to wait until the 5.9 release cycle unless we include it in one of the 5.8.x patch releases.

#3 @desrosj
5 years ago

  • Milestone Awaiting Review → 5.9

#4 @gziolo
5 years ago

  • Owner set to gziolo
  • Resolution → fixed
  • Status new → closed

In 51471:

Editor: Conditionally load registered styles for block variations

In WordPress 5.8 we added the ability to only load styles for blocks when these blocks are rendered. However, these optimizations left out block-styles that get added using the register_block_style() function/API.

Props aristath.
Fixes #53616.

#5 @gziolo
5 years ago

  • Version 5.8 → trunk

#8 @SergeyBiryukov
5 years ago

  • Keywords fixed-major added
  • Milestone 5.9 → 5.8.1
  • Resolution fixed
  • Status closed → reopened

Reopening for 5.8.1 consideration per ​Slack disccussion.

#9 @desrosj
5 years ago

It looks like [51417] contains an anonymous function. While these are allowed in some situations, they are not currently allowed for filter and action hooks (​relevant Core coding standard). Can [51417] be refined to not use a closure?

#10 @aristath
5 years ago

This was also mentioned prior to merging the PR, in ​https://github.com/WordPress/wordpress-develop/pull/1482#pullrequestreview-704072508
However, it was deemed acceptable because in this scenario, the anonymous function enqueues a stylesheet and doesn't do anything else:

plugins can still just run wp_dequeue_style to remove the styles. Since they can remove the styles, removing the hook itself seemed inconsequential.

#11 @desrosj
5 years ago

  • Resolution → fixed
  • Status reopened → closed

In 51696:

Editor: Conditionally load registered styles for block variations

In WordPress 5.8 we added the ability to only load styles for blocks when these blocks are rendered. However, these optimizations left out block-styles that get added using the register_block_style() function/API.

Props aristath, gziolo.
Merges [51471] to the 5.8 branch.
Fixes #53616.

Note: See TracTickets for help on using tickets.