Aggressive JavaScript caching

前端 未结 9 958
北荒
北荒 2020-12-24 08:45

I\'ve run into a problem where I make changes to a few JavaScript files that are referenced in an HTML file, but the browser doesn\'t see the changes. It holds onto the copy

相关标签:
9条回答
  • 2020-12-24 09:09

    is your webserver sending the right headers to tell the browser it has a new version? I've also added the date to the querystring before. ie myscripts.js?date=4/14/2008 12:45:03 (only the date would be encoded)

    0 讨论(0)
  • 2020-12-24 09:10

    It holds onto the copy cached in the browser, even though the web server has a newer version.

    This is probably because the HTTP Expires / Cache-Control headers are set.

    http://developer.yahoo.com/performance/rules.html#expires

    I wrote about this here:

    http://www.codinghorror.com/blog/archives/000932.html

    This isn't bad advice, per se, but it can cause huge problems if you get it wrong. In Microsoft's IIS, for example, the Expires header is always turned off by default, probably for that very reason. By setting an Expires header on HTTP resources, you're telling the client to never check for new versions of that resource -- at least not until the expiration date on the Expires header. When I say never, I mean it -- the browser won't even ask for a new version; it'll just assume its cached version is good to go until the client clears the cache, or the cache reaches the expiration date. Yahoo notes that they change the filename of these resources when they need them refreshed.

    All you're really saving here is the cost of the client pinging the server for a new version and getting a 304 not modified header back in the common case that the resource hasn't changed. That's not much overhead.. unless you're Yahoo. Sure, if you have a set of images or scripts that almost never change, definitely exploit client caching and turn on the Cache-Control header. Caching is critical to browser performance; every web developer should have a deep understanding of how HTTP caching works. But only use it in a surgical, limited way for those specific folders or files that can benefit. For anything else, the risk outweighs the benefit. It's certainly not something you want turned on as a blanket default for your entire website.. unless you like changing filenames every time the content changes.

    0 讨论(0)
  • 2020-12-24 09:11

    @Jason and Darren

    IE6 treats anything with a query string as uncacheable. You should find another way to get the version number into the url, such as a fake directory:

    <script src="/js/version/MyScript.js"/>
    

    and just remove that first directory level after js on the server side before fulfilling the request.

    EDIT: Sorry all; it is Squid, not IE6, that won't cache with a query string. More info here.

    0 讨论(0)
  • 2020-12-24 09:19

    I've written a blog post about how we overcame this problem here:

    Avoiding JavaScript and CSS Stylesheet Caching Problems in ASP.NET

    Basically, during development you can add a random number to a query string after the filename of your CSS file. When you do a release build, the code switches to using your assembly's revision number instead. This means that in your production environment, your clients can cache the stylesheet, but whenever you release a new version of the site they'll be forced to re-load the file.

    0 讨论(0)
  • 2020-12-24 09:20

    We append a product build number to the end of all Javascript (and CSS etc.) like so:

    <script src="MyScript.js?4.0.8243">
    

    Browsers ignore everything after the question mark but upgrades cause a new URL which means cache-reload.

    This has the additional benefit that you can set HTTP headers that mean "never cache!"

    0 讨论(0)
  • 2020-12-24 09:20

    Some very useful techniques in here even if you are not planning to use powershell to automate deployment.

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