170

I have a simple html:

<html>
<body>
<head>
<meta charset="utf-8">
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<script src="test.js"></script>
</body>
</html>

In test.js I changed a Javascript function, but my browser is caching this file. How to disable cache for script src?

1
  • 1
    I add timestamps to the script-sources but several users still need to fire F5 or Ctrl+F5 to get the new script. How's that? (Intranet not WWW) Commented Aug 5, 2014 at 7:20

5 Answers 5

244

Add a random query string to the src

You could either do this manually by incrementing the querystring each time you make a change:

<script src="test.js?version=1"></script>

Or if you are using a server side language, you could automatically generate this:

ASP.NET:

<script src="test.js?rndstr=<%= getRandomStr() %>"></script>

More info on cache-busting can be found here:

https://www.curtiscode.dev/post/front-end-dev/what-is-cache-busting

Sign up to request clarification or add additional context in comments.

10 Comments

The alternative solution is to use some kind of build number (like number of commits since the beginning, or the revision number / commit hash). This way you will utilize server-side language to do it automatically, yet not lose the advantages of caching when nothing really changed. But this requires, as mentioned: version control (or some other change tracking) and server-side processing.
@Tadeck Better than that, use a hashed value of the content so that it updates when needed. A new build might be produced but the JS might not change. ASP.NET Bundling and Minification has this built in. curtistimson.co.uk/front-end-dev/what-is-cache-busting/#_hashed
you ALL wrong! <script type="text/javascript" src="test.js?md5=..."></script>
@SébastienGarcia-Roméo That's a valid option. It's one of the options I put forward on my linked post curtistimson.co.uk/post/front-end-dev/what-is-cache-busting
An annoying aspect of this solution is that Chrome will treat this as a new file. If you've set breakpoints for instance, you'll lose all of them on the next reload.
|
66
<script src="test.js?random=<?php echo uniqid(); ?>"></script>

EDIT: Or you could use the file modification time so that it's cached on the client.

<script src="test.js?random=<?php echo filemtime('test.js'); ?>"></script>

3 Comments

You'd better put the "LastWriteTimeUTC" unix-timestamp in there, that way it stays cached as long as the script doesn't change.
Simple but great solution, thanks.
uniqid() would generate new filename every time. I think, the better approach is filemtime function. This will change file version only when file is modified and that is the problem when we make changes in the code and we ask the client to clear the cache.
19

Configure your webserver to send caching control HTTP headers for the script.

Fake headers in the HTML documents:

  1. Aren't as well supported as real HTTP headers
  2. Apply to the HTML document, not to resources that it links to

Comments

17

You can append a queryString to your src and change it only when you will release an updated version:

<script src="test.js?v=1"></script>

In this way the browser will use the cached version until a new version will be specified (v=2, v=3...)

Comments

11

You can add a random (or datetime string) as query string to the url that points to your script. Like so:

<script type="text/javascript" src="test.js?q=123"></script> 

Every time you refresh the page you need to make sure the value of 'q' is changed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.