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: |
|
Owned by: |
|
---|---|---|---|
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
#2
@
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.
#4
@
2 years ago
- Owner set to gziolo
- Resolution set to fixed
- Status changed from new to closed
In 51471:
2 years ago
#6
Committed in https://core.trac.wordpress.org/changeset/51471.
2 years ago
#7
Committed in https://core.trac.wordpress.org/changeset/51471.
#8
@
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
@
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
@
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.
This PR modifies the
enqueue_block_styles_assets
function, adding conditions forwp_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