Skip to content

Commit

Permalink
Playground Block: set style version number (#256)
Browse files Browse the repository at this point in the history
This PR ensures that the style cache will be busted when a new version
is released.

## Why?

The style version is currently displayed as _0.0.1_ which matches
`block.json`.
When a new version of the plugin is released, browsers keep returning
the cached version of the style file which might be outdate.

## How?

The style file is now manually registered and a version is set to the
last modified time of the file. This will ensure that if a new version
is installed, browsers will flush the cache.

## Testing Instructions

<!-- Please include step by step instructions on how to test this PR.
-->
1. Check out the branch.
2. Start a local dev site
3. Open a post edit page and add a playground block
4. In network tools confirm that the style path has a timestamp as a
version (e.g. _build/style-index.css?ver=1715165957_)
  • Loading branch information
bgrgicak committed May 8, 2024
1 parent 428986d commit f1dae3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/wordpress-playground-block/src/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wordpress-playground/playground",
"version": "0.0.1",
"title": "WordPress Playground",
"category": "widgets",
"icon": "wordpress",
Expand Down
20 changes: 18 additions & 2 deletions packages/wordpress-playground-block/wordpress-playground-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,23 @@
exit; // Exit if accessed directly.
}

function playground_demo_block_init() {
register_block_type( __DIR__ . '/build' );
function playground_demo_block_init()
{
$style_css = 'build/style-index.css';
wp_register_style(
'playground-block-style',
plugins_url($style_css, __FILE__),
array(
'wp-components'
),
filemtime(plugin_dir_path(__FILE__) . $style_css),
);

register_block_type(
__DIR__ . '/build',
array(
'style' => 'playground-block-style',
)
);
}
add_action( 'init', 'playground_demo_block_init' );

0 comments on commit f1dae3a

Please sign in to comment.