Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#53616 closed defect (bug) (fixed)

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

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

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.


2 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
2 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
2 years ago

  • Milestone changed from Awaiting Review to 5.9

#4 @gziolo
2 years ago

  • Owner set to gziolo
  • Resolution set to fixed
  • Status changed from new to 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
2 years ago

  • Version changed from 5.8 to trunk

#8 @SergeyBiryukov
2 years ago

  • Keywords fixed-major added
  • Milestone changed from 5.9 to 5.8.1
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 5.8.1 consideration per Slack disccussion.

#9 @desrosj
2 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
2 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
2 years ago

  • Resolution set to fixed
  • Status changed from reopened to 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.