Skip to content

Commit

Permalink
Register Assets: cache bust without filemtime (#29775)
Browse files Browse the repository at this point in the history
* Replaces filemtime when registering package styles.

For production, uses GUTENBERG_VERSION.

For dev, uses microtime().

* Replaces filemtime when registering package scripts.

For production, uses GUTENBERG_VERSION.

For dev, uses microtime().

* Improves comments.

* Use time() instead of microtime().

* For consistency, use SCRIPT_DEBUG as dev/prod flag.

* Check if GUTENBERG_VERSION is defined.

Switches the check around to determine if in production mode:

- GUTENBERG_VERSION is defined
- SCRIPT_DEBUG is not defined || is not true.

Else, defaults to time().

* Removes extra empty line (for phpcs).
  • Loading branch information
Tonya Mork authored Mar 17, 2021
1 parent 23f4ee8 commit 3b2b118
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ function gutenberg_register_vendor_scripts( $scripts ) {
* @param WP_Scripts $scripts WP_Scripts instance.
*/
function gutenberg_register_packages_scripts( $scripts ) {
// When in production, use the plugin's version as the default asset version;
// else (for development or test) default to use the current time.
$default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();

foreach ( glob( gutenberg_dir_path() . 'build/*/index.js' ) as $path ) {
// Prefix `wp-` to package directory to get script handle.
// For example, `…/build/a11y/index.js` becomes `wp-a11y`.
Expand All @@ -244,7 +248,7 @@ function gutenberg_register_packages_scripts( $scripts ) {
? require( $asset_file )
: null;
$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array();
$version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $path );
$version = isset( $asset['version'] ) ? $asset['version'] : $default_version;

// Add dependencies that cannot be detected and generated by build tools.
switch ( $handle ) {
Expand Down Expand Up @@ -285,13 +289,17 @@ function gutenberg_register_packages_scripts( $scripts ) {
* @param WP_Styles $styles WP_Styles instance.
*/
function gutenberg_register_packages_styles( $styles ) {
// When in production, use the plugin's version as the asset version;
// else (for development or test) default to use the current time.
$version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time();

// Editor Styles.
gutenberg_override_style(
$styles,
'wp-block-editor',
gutenberg_url( 'build/block-editor/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
$version
);
$styles->add_data( 'wp-block-editor', 'rtl', 'replace' );

Expand All @@ -300,7 +308,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-editor',
gutenberg_url( 'build/editor/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-nux', 'wp-reusable-blocks' ),
filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
$version
);
$styles->add_data( 'wp-editor', 'rtl', 'replace' );

Expand All @@ -309,7 +317,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-edit-post',
gutenberg_url( 'build/edit-post/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
$version
);
$styles->add_data( 'wp-edit-post', 'rtl', 'replace' );

Expand All @@ -318,7 +326,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-components',
gutenberg_url( 'build/components/style.css' ),
array( 'dashicons' ),
filemtime( gutenberg_dir_path() . 'build/components/style.css' )
$version
);
$styles->add_data( 'wp-components', 'rtl', 'replace' );

Expand All @@ -328,7 +336,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-block-library',
gutenberg_url( 'build/block-library/' . $block_library_filename . '.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' )
$version
);
$styles->add_data( 'wp-block-library', 'rtl', 'replace' );
$styles->add_data( 'wp-block-library', 'path', gutenberg_dir_path() . 'build/block-library/' . $block_library_filename . '.css' );
Expand All @@ -338,7 +346,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-format-library',
gutenberg_url( 'build/format-library/style.css' ),
array( 'wp-block-editor', 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
$version
);
$styles->add_data( 'wp-format-library', 'rtl', 'replace' );

Expand All @@ -360,7 +368,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-edit-blocks',
gutenberg_url( 'build/block-library/editor.css' ),
$wp_edit_blocks_dependencies,
filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
$version
);
$styles->add_data( 'wp-edit-blocks', 'rtl', 'replace' );

Expand All @@ -369,7 +377,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-nux',
gutenberg_url( 'build/nux/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
$version
);
$styles->add_data( 'wp-nux', 'rtl', 'replace' );

Expand All @@ -378,7 +386,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-block-library-theme',
gutenberg_url( 'build/block-library/theme.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
$version
);
$styles->add_data( 'wp-block-library-theme', 'rtl', 'replace' );

Expand All @@ -387,7 +395,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-list-reusable-blocks',
gutenberg_url( 'build/list-reusable-blocks/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
$version
);
$styles->add_data( 'wp-list-reusable-block', 'rtl', 'replace' );

Expand All @@ -396,7 +404,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-edit-navigation',
gutenberg_url( 'build/edit-navigation/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
filemtime( gutenberg_dir_path() . 'build/edit-navigation/style.css' )
$version
);
$styles->add_data( 'wp-edit-navigation', 'rtl', 'replace' );

Expand All @@ -405,7 +413,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-edit-site',
gutenberg_url( 'build/edit-site/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
filemtime( gutenberg_dir_path() . 'build/edit-site/style.css' )
$version
);
$styles->add_data( 'wp-edit-site', 'rtl', 'replace' );

Expand All @@ -414,7 +422,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-edit-widgets',
gutenberg_url( 'build/edit-widgets/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks', 'wp-reusable-blocks' ),
filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
$version
);
$styles->add_data( 'wp-edit-widgets', 'rtl', 'replace' );

Expand All @@ -423,7 +431,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-block-directory',
gutenberg_url( 'build/block-directory/style.css' ),
array( 'wp-block-editor', 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/block-directory/style.css' )
$version
);
$styles->add_data( 'wp-block-directory', 'rtl', 'replace' );

Expand All @@ -432,7 +440,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-customize-widgets',
gutenberg_url( 'build/customize-widgets/style.css' ),
array( 'wp-components', 'wp-block-editor', 'wp-edit-blocks' ),
filemtime( gutenberg_dir_path() . 'build/customize-widgets/style.css' )
$version
);
$styles->add_data( 'wp-customize-widgets', 'rtl', 'replace' );

Expand All @@ -441,7 +449,7 @@ function gutenberg_register_packages_styles( $styles ) {
'wp-reusable-blocks',
gutenberg_url( 'build/reusable-blocks/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/reusable-blocks/style.css' )
$version
);
$styles->add_data( 'wp-reusable-block', 'rtl', 'replace' );
}
Expand Down

0 comments on commit 3b2b118

Please sign in to comment.