简体   繁体   中英

Force refresh of client browser

I have added a pretty substantial change to a javascript file. However, in order for the new changes to register, I need to refresh my browser. I imagine this is how it will be for all the other people using this site as well.

Is there a way to invalidate the client's browser cache and force it to download the new js file?

您可以重命名文件,或在加载文件时添加一个随机参数:

<script src="/javascripts/application.js?version=4" type="text/javascript"></script>

您可以在文件名中添加参数,例如

<script src="script.js?rnd=2131231"></script>

There is a very comprehensive discussion here , with several ways to do this.
The recommended solution (mod_pagespeed) might not be the best solution for you in case your project is not big enough or you don't use apache, in which case I would recommend a format such as:

<script src="/{sha_1_here}/script.js"></script>

instead of a GET parameter, because apparently some browsers do not cache scripts with parameters.
This should work everywhere, and you just need to rewrite the URL server side.

For development purposes, I disable the cache on the browser side so that it fetches always.

But for production, to reset so that all client user's browsers should download the new version you can look at apache's settings to expire cached files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM