WordPress.org

Make WordPress Core

Opened 2 months ago

#43825 new enhancement

Style/script loading infrastructure: Etag header as a hash of script/style handles and their corresponding versions

Reported by: kanishk.dudeja Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: Script Loader Keywords:
Focuses: Cc:

Description

We've been exploring on how to bundle styles and scripts together for different block types in Gutenberg (https://github.com/WordPress/gutenberg/pull/6087#issuecomment-382888928)

It looks like the inbuilt infrastructure for concatenating and bundling assets (load-styles.php and load-scripts.php) might be a good fit for bundling assets in Gutenberg.

However, it looks like we will run into an issue with this approach. load-styles.php and load-scripts.php use the current WordPress version as the value of the Etag header. So if a plugin/theme developer modifies some CSS / JS for their block type, the browser will not attempt to fetch the latest content since the value of the Etag header will remain the same (since the WordPress version will remain the same).

We can add some functionality in load-styles.php and load-scripts.php to support this:

Specify the value of the Etag header as a hash of the script/style handles and their corresponding versions.

So, if we get a request to load styles for paragraph and image block like this:

/load-styles.php?load=core-paragraph-block,core-image-block

We can generate the Etag like this:

etag = hash_function( 'core-paragraph-block-v1.1-core-image-block-v8.3')

and supply this value in the Etag header in the response to the browser.

This will make sure that the next time the plugin/theme developer changes the CSS / JS for a block type (meaning changes the version of the script/style handle), the hash function will automatically generate a new value for the Etag header and the browser will therefore fetch the latest style / script bundle.

We can add this functionality in load-styles.php and load-scripts.php as an add-on to the current functionality (to preserve backward compatibility).

Maybe, this new functionality can be triggered if a specific URL parameter is included in the request to load-styles.php / load-scripts.php. Maybe, something like: /load-styles.php?etag=content_hash.

Do you think we can go ahead with this approach? If yes, I can propose a patch for adding this functionality.

Attachments (1)

43825.patch (4.3 KB) - added by kanishk.dudeja 2 months ago.
A patch illustrating the enhancement

Download all attachments as: .zip

Change History (1)

@kanishk.dudeja
2 months ago

A patch illustrating the enhancement

Note: See TracTickets for help on using tickets.