Disable JavaScript caching in Google Chrome
Asked Answered
M

1

6

Is it possible to disable JavaScript caching in Google Chrome? I turned off the cache in developer tools (Ctrl-Shift-I), but it still caches JS files... Using Chrome version 20.

Malley answered 14/6, 2012 at 8:52 Comment(0)
H
8

For developing purposes you can use Ctrl+R or Ctrl+F5, they should not send cache headers therefore requesting the resource from server and not cache. This is called Hard Refresh.

But don't expect users to use Ctrl+R/F5, if you want to always send the uncached resource to the user, use Cache buster (Google search for cache buster)

Web browsers use the url to determine if they already know the resource, so accessing the same url will make the web browser to check if he accessed this url before thus allowing it to send a specific header of the resource it have and validate if the resource changed on the server.

Cache busters are as the name says used to bust the cache and always reload the resource. Here is an example for cache busted url:

<script type="text/javascript" src="/static/js/some.js?bust=12356"></script>

Note: it could be any name and not "bust".

A good way to always have a unique bust is using the Unix epoch time (number of seconds passed from 1.1.1970, every language offers you some function to get that number), another commonly used solution is some random number. This way the browser will always get the resource from the web server.

Henryson answered 14/6, 2012 at 9:17 Comment(4)
I didn't downvote you, but links are not answers. At the very least put an example of a "cache buster" (or to put it in a layman's terms, just attach a randomly generated query string to the src attribute which doesn't modify the file at all and forces it to be re-downloaded every time).Chihli
@Fabrício Matté Thank you for your critic! I always try to improve my self and my answers and to give back to the community as much as I can. I modified my answer to have some basic theoretical explanation about web browser cache and examples of cache buster.Henryson
Query is not guaranteed to "bust" cache. IE, for example, will happily continue serve cached content if not explicitly prohibited by headers.Tirzah
I see, +1 now. Also, a good practice is to imagine as if all of the links in your answer were not available anymore, so it's always better to put some code in your answers for future readers. :PChihli

© 2022 - 2024 — McMap. All rights reserved.