How to programmatically empty browser cache?

前端 未结 11 985
迷失自我
迷失自我 2020-11-22 17:36

I am looking for a way to programmatically empty the browser cache. I am doing this because the application caches confidential data and I\'d like to remove those when you p

11条回答
  •  臣服心动
    2020-11-22 17:54

    Imagine the .js files are placed in /my-site/some/path/ui/js/myfile.js

    So normally the script tag would look like:

    
    

    Now change that to:

    
    

    Now of course that will not work. To make it work you need to add one or a few lines to your .htaccess The important line is: (entire .htaccess at the bottom)

    RewriteRule ^my-site\/(.*)\/ui\-([0-9]+)\/(.*) my-site/$1/ui/$3 [L]
    

    So what this does is, it kind of removes the 1111111111 from the path and links to the correct path.

    So now if you make changes you just have to change the number 1111111111 to whatever number you want. And however you include your files you can set that number via a timestamp when the js-file has last been modified. So cache will work normally if the number does not change. If it changes it will serve the new file (YES ALWAYS) because the browser get's a complete new URL and just believes that file is so new he must go get it.

    You can use this for CSS, favicons and what ever gets cached. For CSS just use like so

    
    

    And it will work! Simple to update, simple to maintain.

    The promised full .htaccess

    If you have no .htaccess yet this is the minimum you need to have there:

    
        RewriteEngine On
        RewriteBase /
    
        RewriteRule ^my-site\/(.*)\/ui\-([0-9]+)\/(.*) my-site/$1/ui/$3 [L]
    
    

提交回复
热议问题