Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying Updated Javascript Files

If I want to deploy a bug fix to an existing JS file, how do I ensure the old one is not cached on my clients' computers?

like image 794
Joda Maki Avatar asked Feb 21 '11 21:02

Joda Maki


3 Answers

Normally your static resources should be versionned and served through urls like /static.js?ver=123. So all you would need is to increment the version number. Take for example this very same site, view the source and you will notice the following in the <head> section:

<script type="text/javascript" src="http://sstatic.net/js/master.min.js?v=689f646cde8d"></script>
like image 108
Darin Dimitrov Avatar answered Oct 17 '22 06:10

Darin Dimitrov


If everything is compliant with the specs, you shouldn't need to do anything special. HTTP already has mechanisms to handle this. The browser should send an If-Modified-Since header with the retrieval date or If-None-Match header with the ETag of the cached copy. The server will check the modification time (or the ETag, which should include the modification time in the calculation) of the file on the server and, if the mod time is less than that given by the browser, or the ETag matches, return a 304 Not Modified response. The browser should use its cached copy only when it receives a 304 response.

The one wrinkle in this is caching proxies that either ignore any server-provided Expires header or assume it to be well in the future when not given, in which case you can send a Cache-Control header with the "must-revalidate" directive.

Of course, browsers or proxies may not be compliant, in which case the other answers show you how to force a browser to re-fetch a resource.

like image 40
outis Avatar answered Oct 17 '22 04:10

outis


If you are using php

<link rel="stylesheet" 
    href="<?php echo './general.css?' . filemtime('./general.css'); ?>"
    type="text/css" media="screen, projection" />

and forget the problem for ever, it will add the ? followed by the file modification time automatically which will be different every time you change the file.

With other languages should be similar.

like image 24
Miquel Avatar answered Oct 17 '22 04:10

Miquel