Is it the filename or the whole URL used as a key in browser caches?

前端 未结 10 1175
旧巷少年郎
旧巷少年郎 2020-12-15 04:24

It\'s common to want browsers to cache resources - JavaScript, CSS, images, etc. until there is a new version available, and then ensure that the browser fetches and caches

相关标签:
10条回答
  • 2020-12-15 05:04

    Browser cache key is a combination of the request method and resource URI. URI consists of scheme, authority, path, query, and fragment.

    Relevant excerpt from HTTP 1.1 specification:

    The primary cache key consists of the request method and target URI. However, since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key.

    Relevant excerpt from URI specification:

    The generic URI syntax consists of a hierarchical sequence of components referred to as the scheme, authority, path, query, and fragment.

    URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
    
    hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty
    
    0 讨论(0)
  • 2020-12-15 05:04

    Entire url. I've seen a strange behavior in a few older browsers where case sensitivity came into play.

    0 讨论(0)
  • 2020-12-15 05:08

    depends. it is supposed to be the full URL, but some browsers (Opera, Safari2) apply a different cache strategy for urls with different params.

    best bet is to change the name of the file.

    There is a very clever solution here (uses PHP, Apache)

    http://verens.com/archives/2008/04/09/javascript-cache-problem-solved/

    Strategy notes: “According the letter of the HTTP caching specification, user agents should never cache URLs with query strings. While Internet Explorer and Firefox ignore this, Opera and Safari don’t - to make sure all user agents can cache your resources, we need to keep query strings out of their URLs.”

    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast

    0 讨论(0)
  • 2020-12-15 05:11

    I am 99.99999% sure that it is the entire url that is used to cache resources in a browser, so your url scheme should work out fine.

    0 讨论(0)
  • 2020-12-15 05:16

    Of course it has to use the whole path '/r20/example.js' vs '/r21/example.js' could be completely different images to begin with. What you suggest is a viable way to handle version control.

    0 讨论(0)
  • 2020-12-15 05:18

    Yes, any change in any part of the URL (excluding HTTP and HTTPS protocols changes) is interpreted as a different resource by the browser (and any intermediary proxies), and will thus result in a separate entity in the browser-cache.

    Update:

    The claim in this ThinkVitamin article that Opera and Safari/Webkit browsers don't cache URLs with ?query=strings is false.

    Adding a version number parameter to a URL is a perfectly acceptable way to do cache-busting.

    What may have confused the author of the ThinkVitamin article is the fact that hitting Enter in the address/location bar in Safari and Opera results in different behavior for URLs with query string in them.

    However, (and this is the important part!) Opera and Safari behave just like IE and Firefox when it comes to caching embedded/linked images and stylesheets and scripts in web pages - regardless of whether they have "?" characters in their URLs. (This can be verified with a simple test on a normal Apache server.)

    (I would have commented on the currently accepted answer if I had the reputation to do it. :-)

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