0

Im creating a block plugin in worpress and each time I update my JS files, changes do not reflect to frontend because there's a browser cache.

I found some strategies in others frameworks that rename all JS files to a random name every time is compiled by webpack or similar.

Is there a way to achieve same result using WP build script and wp_enqueue_script function?

5
  • 1
    lots of time I use the plugin version as file version. then files are called again instead of being read of the cache every times the plugin version change.
    – mmm
    Commented Sep 4 at 14:03
  • To add to @mmm's suggestion, you could set the $ver parameter in wp_enqueue_script() to time(), at least while you're developing your plugin.
    – Pat J
    Commented Sep 4 at 14:09
  • 1
    note that WP Scripts will automatically generate a hash of the built asset that can be used to solve this, likely using the same approach you've seen other frameworks do and the same webpack tooling. Some might use filemtime also
    – Tom J Nowell
    Commented Sep 4 at 15:02
  • @TomJNowell seems that filetime approach is easier to implement and give same result. Commented Sep 4 at 18:01
  • I have a feeling that the currect answer is a combination of both current answers. Commented Sep 5 at 13:58

2 Answers 2

2

The function wp_enqueue_script takes a parameter $ver that you can use to automatically add an version parameter to the URL. To always load the newest version of your Javascript-File, you can use the php filemtime-function to give the timestamp of the modification date.

In a Plugin, use like this:

wp_register_script( 'my-awesome-script', plugin_dir_url( __FILE__ ). 'assets/my-awesome-script.js', array(), filemtime( plugin_dir_path( __FILE__ ). 'assets/my-awesome-script.js' ), true );

Happy Coding!

3
  • note that on some hosts that use git deployments or containers this could result in new file modified times that unintentionally flush browser cache whenever a change is deployed or containers are reprovisioned, or even different filetimes between different containers/servers
    – Tom J Nowell
    Commented Sep 4 at 19:48
  • and there is nothing wrong with that @TomJNowell ;) Its unlikely that a user on production enviroment will run into any of those cases, and as developer you probably want cache off most of the time. Question is realy what are the chances of a file time to be the same while files are different (I have no idea, but I assume it is close to zero on most enviroments) A bigger problem might be when you are load balancing and upgrading one server at a time. Commented Sep 5 at 13:54
  • I mentioned it as I've seen this happen on some multi-server hosts in production
    – Tom J Nowell
    Commented Sep 5 at 16:01
0

Keep constant in main file, and keep changing it when releasing new version, here's the code you can use.

define( MY_PLUGIN_VERSION, '1.0.0' );

function load_plugin_scripts() {

    wp_enqueue_script( 'handler', plugin_dir_url( __FILE__ ) . '/js/example.js', array(), MY_PLUGIN_VERSION, true );

}

add_action( 'wp_enqueue_scripts', 'load_plugin_scripts' );
2
  • this is the more robust answer, but it requires too much "manual" effort when all you want to do is test some small JS modification. Commented Sep 5 at 13:55
  • I understand that, during development we don't change the version, we hard refresh page by pressing CTRL + F5. Commented Sep 10 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.