cache - Enqueue script with version number not working

I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, t

I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, the ?ver= query is not being appended to the script name in the html and therefore cache busting is failing.

The php code:

wp_enqueue_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true);

And the html tag that is being produced (with identifiable info removed):

<script type="text/javascript" src="/wp-content/plugins/plugin-name/dist/client.js"></script>

Any help or explanation is appreciated, thanks!

I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, the ?ver= query is not being appended to the script name in the html and therefore cache busting is failing.

The php code:

wp_enqueue_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true);

And the html tag that is being produced (with identifiable info removed):

<script type="text/javascript" src="https://site.url/wp-content/plugins/plugin-name/dist/client.js"></script>

Any help or explanation is appreciated, thanks!

Share Improve this question edited May 20, 2019 at 21:02 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 20, 2019 at 20:47 RyanRyan 31 silver badge2 bronze badges 3
  • What's your wp_register_script() for that script? – Derek Held Commented May 20, 2019 at 20:53
  • Where am I supposed to put it? I can have wp_register_script('bundle', plugins_url('dist/client.js', __FILE__), array(), '20190520430', true); wp_enqueue_script('bundle'); and it results in the same thing? – Ryan Commented May 20, 2019 at 21:52
  • You can enqueue and register at the same time with wp_enqueue_script, don't worry about not calling wp_register_script. Ryan, is this happening with other scripts too? Or just this one? – Tom J Nowell Commented May 21, 2019 at 0:21
Add a comment  | 

1 Answer 1

Reset to default 0

You don't strictly need wp_register_script() but it is considered a best practice to use it as it allows other developers to programmatically work with the order and list of scripts being included.

Regarding your issue, your code looks correct. Be sure to call it inside the wp_enqueue_scripts action (but I don't think that's influencing the behavior you are seeing).

If I had to guess, and I kind of do, I would guess that there is some other plugin or system configuration that is stripping the query string as part of a caching mechanism. The thought is that query string parameters, if they are present, can keep some proxy servers, and possibly browsers, from caching the resource. There are even some plugins such as Ratify and possibly W3 Total Cache that will strip query string parameters from resources for you, intentionally. The way to test if another plugin is interfering is by disabling them and seeing if anything changes.

If you are using a caching server or CDN, they may also be stripping the query string (but seems unlikely). Cloudflare offers a maintenance mode that allows you to briefly disable the CDN while you test things.

HTH and please let me know how you get on.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478763a4629466.html

相关推荐

  • cache - Enqueue script with version number not working

    I am trying to enqueue a script for a custom plugin and I want to include a version number for cache busting. However, t

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信