how to clear my clients cache next time they log in

后端 未结 6 1744
萌比男神i
萌比男神i 2021-01-11 18:27

I have an application running on hostgator. When I make changes to the js files my users don\'t see the changes until they clear their cache. Is this the only option to pu

相关标签:
6条回答
  • 2021-01-11 18:50

    The trick I use to avoid this issue is to modify the url of the updated script. This will force the client to download the script again as the ressource name is different.

    Easiest way to do it : use a version number at the end of the url js/my-script.js?v=1.0.

    Better way to do it (100% supported for all navigators): change the file name itself js/my-script.v1.0.js.

    0 讨论(0)
  • 2021-01-11 18:53

    One trick to get around this is to append a query string with some kind of version or timestamp to your link Javascript calls. For example:

    <script src="myexternal.js?20130116" type="text/javascript"></script>
    

    This example is simple, but proves the point. Basically this tells the browser to load the file if it doesn't recognize the full path. Anytime you change your javascript file and want your users to pickup the latest version, just update that timestamp or value after the question mark (query string).

    0 讨论(0)
  • 2021-01-11 18:54

    A simple trick I use sometimes is to add some dummy parameters to the path of the JavaScript/CSS file. The browser will essentially see a different URL and will not use it's cached version.

    $jsFile = '/js/myFile.js';                // probably cached
    $jsFile = '/js/myFile.js?r='.time();      // won't ever be cached
    

    You can't really clear the clients cache, but you can trick the clients browser into thinking that this is the first time it is seeing this file and it will download it again.

    Don't forget to disable this once you have arrived at a stable version. Caching files is a great way to speed up an application's load time.

    0 讨论(0)
  • 2021-01-11 19:00

    You need to use versioning on your file includes. Anytime you change the URI of your file includes, the browser won't find a cache match and will re-download the include.

    For example:

    <script type="text/javascript" src="include/232/init.js"></script>
    

    Where 232 is your modifiable version number that should be changed whenever you release new code.

    Alternatively, you can use query strings:

    <script type="text/javascript" src="include/init.js?232"></script>
    

    The point is that you should change the file include URI in some manner whenever you want your visitors to re-download the file.

    If you use PHP or another server-side language, you can setup this versioning to occur automatically whenever your file includes are modified: http://derek.io/blog/2009/auto-versioning-javascript-and-css-files/

    0 讨论(0)
  • 2021-01-11 19:01

    You can't clear the cache of your client's browsers but you can use cache busting techniques such as adding versioning to your files

    <link rel="stylesheet" href="/css/example.css?v1.0" type="text/css" />
    

    And then when you make a change

    <link rel="stylesheet" href="/css/example.css?v1.1" type="text/css" />
    

    It can be a timestamp or whatever, just needs to be different.

    0 讨论(0)
  • 2021-01-11 19:10

    You can append dummy query strings to the asset URL:

    <link href="http://mysite.com/main.css?v=1" rel="stylesheet" />
    

    Then, whenever you make any changes to your CSS, just bump up the version number.

    This technique is called cache busting. Search Google for that, and you'll get many different variations.

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