Make WordPress Core

Opened 3 weeks ago

Last modified 5 days ago

#63254 new defect (bug)

Introduce development mode for block editor styles

Reported by: helgatheviking's profile helgatheviking Owned by:
Milestone: 6.8.2 Priority: normal
Severity: normal Version: 5.5
Component: Editor Keywords: has-patch
Focuses: Cc:

Description (last modified by sabernhardt)

When a block script is defined in the block.json and registered automatically via register_block_script_handle() the version is pulled from the script asset PHP file if it exists. This ensures that the cache is always busted when the file is updated.

Something similar should exist for register_block_style_handle as otherwise it's a total pain to develop editor styles. I just lost a few hours this morning wondering why my styles didn't update even though I am using the @wordpress/scripts package to recompile them on every change.

The answer was ultimately that the version is pulled from the block.json's "version" property which doesn't change while building.

Replacing this:

$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$result  = wp_register_style(
	$style_handle_name,
	$style_uri,
	array(),
	$version
);

with this:

$block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$version = SCRIPT_DEBUG ? filemtime( $style_path_norm ) : $block_version;

$result  = wp_register_style(
	$style_handle_name,
	$style_uri,
	array(),
	$version
);

Should do the trick. The assets file is another option.

Attachments (2)

63254.patch (883 bytes) - added by abcd95 3 weeks ago.
63254-v2.patch (2.1 KB) - added by gziolo 7 days ago.
A version to adds the same changes for scripts and styles.

Download all attachments as: .zip

Change History (16)

#1 @abcd95
3 weeks ago

Hi @helgatheviking, Thanks for bringing this up.

I agree that the lack of automatic cache busting during development is problematic on many levels, especially when actively making styling changes and not seeing them reflected.

I will follow up with a proper patch; let me know if it tests well for you.

@abcd95
3 weeks ago

#2 @abcd95
3 weeks ago

  • Keywords has-patch needs-testing added

#3 @abcd95
3 weeks ago

The change was first introduced in 56044 and mainly focused on performance improvements.

#4 @sabernhardt
3 weeks ago

  • Component changed from General to Editor
  • Description modified (diff)

#5 @helgatheviking
3 weeks ago

@abcd95 that patch works great for me!

#6 @gziolo
11 days ago

That would be great to fix it. I think the logic should be unified between JS and CSS assets. Something like that should work best:

<?php
$default_version = SCRIPT_DEBUG ? time() : false; 
$block_version   = isset( $metadata['version'] ) ? $metadata['version'] : $default_version;
$script_version  = isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version;       

This would cover all possible cases including where the block.json and script.asset.php don't contain tje version property. For styles, everything should look exactly the same but for the style asset when defined. However, it could be break down into two steps as today, reading style asset is not covered. The related issue is tracked in Gutenberg: https://github.com/WordPress/gutenberg/issues/56838.

#7 @helgatheviking
10 days ago

I wasn't sure if the script version updated if changes only happened to the scss. But if so, that sounds great to me.

@gziolo
7 days ago

A version to adds the same changes for scripts and styles.

#8 follow-up: @gziolo
7 days ago

After taking a closer look, I think what @abcd95 provided is the best way to go forward. I extended that for scripts and script modules. The logic covers the case when the asset file doesn't contain version which changes based on the file's content. In every other case when SCRIPT_DEBUG is enabled the version should be randomized on every page load to ensure it never gets served from the cache.

#9 @gziolo
7 days ago

  • Milestone changed from Awaiting Review to 6.8.1

#10 @gziolo
7 days ago

  • Version set to 5.5

This ticket was mentioned in PR #8725 on WordPress/wordpress-develop by @abcd95.


6 days ago
#11

#12 in reply to: ↑ 8 @abcd95
6 days ago

  • Keywords needs-testing removed

Replying to gziolo:

After taking a closer look, I think what @abcd95 provided is the best way to go forward.

Thanks @gziolo for reviewing and @helgatheviking for testing the patch. I'm opening a PR to run and check the automated tests and making the review easier.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


5 days ago

#14 @jorbin
5 days ago

  • Milestone changed from 6.8.1 to 6.8.2

In order to give this time for testing and to keep 6.8.1 focused on issues introduced during the 6.8 cycle or intentionally deferred at the end of the 6.8 cycle, I've moved this to 6.8.2

Note: See TracTickets for help on using tickets.