How to make browser stop caching GWT nocache.js

browser-cache, gwt, lighttpd

Solution

The best way to avoid browser caching is set the expiration time to now and add the max-age=0 and the must-revalidate controls.

This is the configuration I use with apache-httpd

ExpiresActive on
<LocationMatch "nocache">
   ExpiresDefault "now"
   Header set Cache-Control "public, max-age=0, must-revalidate"
</LocationMatch>
<LocationMatch "\.cache\.">
   ExpiresDefault "now plus 1 year"
</LocationMatch>

your configuration for lighthttpd should be

server.modules = (
    "mod_expire",
    "mod_setenv",
)
...
$HTTP["url"] =~ "\.nocache\." {
  setenv.add-response-header = ( "Cache-Control" => "public, max-age=0, must-revalidate" )
  expire.url = ( "" => "access plus 0 days" )
}

$HTTP["url"] =~ "\.cache\." {
  expire.url = ( "" => "access plus 1 years" )
}

Problem

I'm developing a web app using GWT and am seeing a crazy problem with caching of the `app.nocache.js` file in the browser even though the web server sent a new copy of the file! I am using Eclipse to compile the app, which works in dev mode. To test production mode, I have a virtual machine (Oracle VirtualBox) with a Ubuntu guest OS running on my host machine (Windows 7). I'm running lighttpd web server in the VM. The VM is sharing my project's war directory, and the web server is serving this dir. I'm using Chrome as the browser, but the same thing happens in Firefox. Here's the scenario: - The web page for the app is blank. Accorind to Chrome's "Inspect Element" tool, it's because it is trying fetch `6E89D5C912DD8F3F806083C8AA626B83.cache.html`, which doesn't exist (`404 not found`). - I check the war directory, and sure enough, that file doesn't exist. - The `app.nocache.js` on the browser WAS RELOADED from the web server (200 OK), because the file on the server was newer than the browser cache. I verified that file size and timestamp for the new file returned by the server were correct. (This is info Chrome reports about the server's HTTP response) However, if I open the `app.nocache.js` on the browser, the javascript is referring to `6E89D5C912DD8F3F806083C8AA626B83.cache.html`!!! That is, even though the web server sent a new `app.nocache.js`, the browser seems to have ignored that and kept using its cached copy! Goto Google->GWT Compile in Eclipse. Recompile the whole thing. - Verify in the war directory that the `app.nocache.js` was overwritten and has a new timestamp. - Reload the page from Chrome and verify once again that the server sent a 200 OK response to the `app.nocache.js`. - The browser once again tries to load `6E89D5C912DD8F3F806083C8AA626B83.cache.html` and fails. The browser is still using the old cached copy of `app.nocache.js`. - Made absolutely certain in the war directory that nothing is referring to `6E89D5C912DD8F3F806083C8AA626B83.cache.html` (via find and grep) What is going wrong? Why is the browser caching this `nocache.js` file even when the server is sending it a new copy? Here is a copy of the HTTP request/response headers when clicking reload in the browser. In this trace, the server content hasn't been recompiled since the last GET (but note that the cached version of nocache.js is still wrong!): ``` Request URL:http://192.168.2.4/xbts_ui/xbts_ui.nocache.js Request Method:GET Status Code:304 Not Modified Request Headersview source Accept:*/* Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Host:192.168.2.4 If-Modified-Since:Thu, 25 Oct 2012 17:55:26 GMT If-None-Match:"2881105249" Referer:http://192.168.2.4/XBTS_ui.html User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 Response Headersview source Accept-Ranges:bytes Content-Type:text/javascript Date:Thu, 25 Oct 2012 20:27:55 GMT ETag:"2881105249" Last-Modified:Thu, 25 Oct 2012 17:55:26 GMT Server:lighttpd/1.4.31 ```

Original source

Related problems