Force browser to clear cache

后端 未结 17 2082
夕颜
夕颜 2020-11-22 05:04

Is there a way I can put some code on my page so when someone visits a site, it clears the browser cache, so they can view the changes?

Languages used: ASP.NET, VB.N

相关标签:
17条回答
  • 2020-11-22 05:48

    Look into the cache-control and the expires META Tag.

    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">

    Another common practices is to append constantly-changing strings to the end of the requested files. For instance:

    <script type="text/javascript" src="main.js?v=12392823"></script>

    0 讨论(0)
  • 2020-11-22 05:51

    Not sure if that might really help you but that's how caching should work on any browser. When the browser request a file, it should always send a request to the server unless there is a "offline" mode. The server will read some parameters like date modified or etags.

    The server will return a 304 error response for NOT MODIFIED and the browser will have to use its cache. If the etag doesn't validate on server side or the modified date is below the current modified date, the server should return the new content with the new modified date or etags or both.

    If there is no caching data sent to the browser, I guess the behavior is undetermined, the browser may or may not cache file that don't tell how they are cached. If you set caching parameters in the response it will cache your files correctly and the server then may choose to return a 304 error, or the new content.

    This is how it should be done. Using random params or version number in urls is more like a hack than anything.

    http://www.checkupdown.com/status/E304.html http://en.wikipedia.org/wiki/HTTP_ETag http://www.xpertdeveloper.com/2011/03/last-modified-header-vs-expire-header-vs-etag/

    After reading I saw that there is also a expire date. If you have problem, it might be that you have a expire date set up. In other words, when the browser will cache your file, since it has a expiry date, it shouldn't have to request it again before that date. In other words, it will never ask the file to the server and will never receive a 304 not modified. It will simply use the cache until the expiry date is reached or cache is cleared.

    So that is my guess, you have some sort of expiry date and you should use last-modified etags or a mix of it all and make sure that there is no expire date.

    If people tends to refresh a lot and the file doesn't get changed a lot, then it might be wise to set a big expiry date.

    My 2 cents!

    0 讨论(0)
  • 2020-11-22 05:54

    Updating the URL to the following works for me:

    /custom.js?id=1

    By adding a unique number after ?id= and incrementing it for new changes, users do not have to press CTRL + F5 to refresh the cache. Alternatively, you can append hash or string version of the current time or Epoch after ?id=

    Something like ?id=1520606295

    0 讨论(0)
  • 2020-11-22 05:54

    Do you want to clear the cache, or just make sure your current (changed?) page is not cached?

    If the latter, it should be as simple as

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    
    0 讨论(0)
  • 2020-11-22 05:55

    If this is about .css and .js changes, one way is to to "cache busting" is by appending something like "_versionNo" to the file name for each release. For example:

    script_1.0.css // This is the URL for release 1.0
    script_1.1.css // This is the URL for release 1.1
    script_1.2.css // etc.
    

    Or alternatively do it after the file name:

    script.css?v=1.0 // This is the URL for release 1.0
    script.css?v=1.1 // This is the URL for release 1.1
    script.css?v=1.2 // etc.
    

    You can check out this link to see how it could work.

    0 讨论(0)
  • 2020-11-22 05:56

    In addition to setting Cache-control: no-cache, you should also set the Expires header to -1 if you would like the local copy to be refreshed each time (some versions of IE seem to require this).

    See HTTP Cache - check with the server, always sending If-Modified-Since

    0 讨论(0)
提交回复
热议问题