3

On what basis does javascript files get cached? Say I load a file with the name 'm-script.js' from one site and on another website I use the same name 'm-script.js' but with different contents. Will the browser fetch the new one, or just look at the name and load it from the cache? The urls for both the m-script.js file are different (obviously).
Thanks.

  • I also found that the web server adds and [ETag](http://en.wikipedia.org/wiki/HTTP_ETag) which is part of HTTP which is also used for cache validation. –  May 31 '12 at 06:03

3 Answers3

4

If the url is different the cached copy will not be used. A new request will be made and the new file will be downloaded.

There would be a huge security and usability issue with the browser if a Javascript file cached from one website was used on another.

Paul
  • 139,544
  • 27
  • 275
  • 264
2

Browsers cache files by their full URI.

This thread( How to force browser to reload cached CSS/JS files? ) will help you to understand.

Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
1

Since nobody has mentioned it yet, there is a lot more involved in HTTP caching than just the URI. There are various headers that control the process, e.g. Cache-Control, Expires, ETag, Vary, and so on. Requesting a different URI is always guaranteed to fetch a new copy, but these headers give more control over how requests to the potentially-cached resource are issued (or not issued, or issued but receive back a 304 Not Modified, or...).

Here is a detailed document describing the process. You can also google things like "caching expires" or "caching etag" for some more specific resources.

Domenic
  • 110,262
  • 41
  • 219
  • 271