0

I wanted to know how using a random number makes a difference when included in a file name which is being referenced in a html.

How is the first line of code different from the second one?

<script src="index.js?1481269289258"></script>

<script src="index.js"></script>

Any details/info on this would help.

Thank you all very much.

SLePort
  • 15,211
  • 3
  • 34
  • 44
  • 1
    Check out this answer. http://stackoverflow.com/questions/23603023/file-caching-query-string-vs-last-modified – 0xcaff Dec 23 '16 at 06:47
  • The number, is usually in the form of a unix timestamp or version. This is appended to force the web browser to load a new (fresh) copy of the file as `foo.js?12345` is a different file than `foo.js?12346`, the browser would re-download `foo.js` updated copy replacing the locally cached version if any. It's really a hack to trick browsers into reloading your script when they are too stubborn to give up the old copy. – Kraang Prime Dec 23 '16 at 06:49

2 Answers2

2

This is sometimes used to bypass caching. Typically the http server or caching layer (e.g. varnish) use query parameters in the cache key. Thus, adding a unique query parameter will serve the same file, but force a reload.

rofls
  • 4,993
  • 3
  • 27
  • 37
  • ... or to ensure that only the latest version of the script is cached (not forcing the browser to cache, but to ditch the old version if it had a cached copy and update with the new version) -- it doesn't actually prevent caching. just bypasses the stubbornness of browser caches in most cases. – Kraang Prime Dec 23 '16 at 06:50
  • Yes, definitely. Though there you would typically use a versioning system, not a random number or string. – rofls Dec 23 '16 at 06:51
  • Random wouldn't help in that case :) – rofls Dec 23 '16 at 06:52
  • 1
    Correct :) (also tweaked my comment above) – Kraang Prime Dec 23 '16 at 06:52
  • Thanks Guys. So basically a cache buster? – allenjoel Dec 23 '16 at 06:57
1

The random number force the navigator to reload the script. Otherwise it may run the script in cache.

Fred B
  • 142
  • 1
  • 8