How to force the browser to reload cached CSS/JS files?

后端 未结 30 4265
青春惊慌失措
青春惊慌失措 2020-11-21 05:49

I have noticed that some browsers (in particular, Firefox and Opera) are very zealous in using cached copies of .css and .js files, even be

相关标签:
30条回答
  • 2020-11-21 05:49

    For ASP.NET I suppose next solution with advanced options (debug/release mode, versions):

    Js or Css files included by such way:

    <script type="text/javascript" src="Scripts/exampleScript<%=Global.JsPostfix%>" />
    <link rel="stylesheet" type="text/css" href="Css/exampleCss<%=Global.CssPostfix%>" />
    

    Global.JsPostfix and Global.CssPostfix is calculated by the following way in Global.asax:

    protected void Application_Start(object sender, EventArgs e)
    {
        ...
        string jsVersion = ConfigurationManager.AppSettings["JsVersion"];
        bool updateEveryAppStart = Convert.ToBoolean(ConfigurationManager.AppSettings["UpdateJsEveryAppStart"]);
        int buildNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Revision;
        JsPostfix = "";
    #if !DEBUG
        JsPostfix += ".min";
    #endif      
        JsPostfix += ".js?" + jsVersion + "_" + buildNumber;
        if (updateEveryAppStart)
        {
            Random rand = new Random();
            JsPosfix += "_" + rand.Next();
        }
        ...
    }
    
    0 讨论(0)
  • 2020-11-21 05:50

    Instead of changing the version manually, I would recommend you use an MD5 hash of the actual CSS file.

    So your URL would be something like

    http://mysite.com/css/[md5_hash_here]/style.css
    

    You could still use the rewrite rule to strip out the hash, but the advantage is that now you can set your cache policy to "cache forever", since if the URL is the same, that means that the file is unchanged.

    You can then write a simple shell script that would compute the hash of the file and update your tag (you'd probably want to move it to a separate file for inclusion).

    Simply run that script every time CSS changes and you're good. The browser will ONLY reload your files when they are altered. If you make an edit and then undo it, there's no pain in figuring out which version you need to return to in order for your visitors not to re-download.

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

    google chrome has Hard Reload as well as Empty Cache and Hard Reload option.You can click and hold the reload button (In Inspect Mode) to select one .

    0 讨论(0)
  • 2020-11-21 05:52

    If you're using git + PHP, you can reload the script from cache each time there is a change in the git repo, using the following code:

    exec('git rev-parse --verify HEAD 2> /dev/null', $gitLog);
    echo '  <script src="/path/to/script.js"?v='.$gitLog[0].'></script>'.PHP_EOL;
    
    0 讨论(0)
  • 2020-11-21 05:53

    Google's mod_pagespeed plugin for apache will do auto-versioning for you. It's really slick.

    It parses HTML on its way out of the webserver (works with PHP, rails, python, static HTML -- anything) and rewrites links to CSS, JS, image files so they include an id code. It serves up the files at the modified URLs with a very long cache control on them. When the files change, it automatically changes the URLs so the browser has to re-fetch them. It basically just works, without any changes to your code. It'll even minify your code on the way out too.

    0 讨论(0)
  • 2020-11-21 05:53

    You can just put ?foo=1234 at the end of your css / js import, changing 1234 to be whatever you like. Have a look at the SO html source for an example.

    The idea there being that the ? parameters are discarded / ignored on the request anyway and you can change that number when you roll out a new version.


    Note: There is some argument with regard to exactly how this affects caching. I believe the general gist of it is that GET requests, with or without parameters should be cachable, so the above solution should work.

    However, it is down to both the web server to decide if it wants to adhere to that part of the spec and the browser the user uses, as it can just go right ahead and ask for a fresh version anyway.

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