GWT nocache.js cached by browser

Recently we faced an issue with the GWT deployment. Whenever we were doing a new deployment the users seemed to get errors as the gwt did not refresh the javascript files even when the user reloaded the page. Then the temporary solution was to reload all the files using Shift+F5.  All our pages had this tag so as to prevent the caching of the appname.nocache.js

<meta http-equiv="Pragma" content="no-cache">

But this did not resolve our problem as the users were still reeling with same errors.
After some more investigation we found out that in the war file the timestamp of appname.nocache.js was not being modified. Since we are using maven for the build we introduced the touch tag. “This is a bug in GWT 2.7”

<touch datetime="now">
    <fileset dir="target"/>
</touch>

This seemed to make things work better but we still had occasions when on redeployment nocahe.js was still not loaded from server but from cache.
All these issues were happening because browser somehow cached the appname.nocacache.js. After googling we found out that there is a better way than the meta tag to force the browser not to cache the nocache.js and that was via the apache server. GWT Perfect Caching is a great read.
Since we were using apache as a proxy for our application. In apache we had the configuration for VirtualHost as below

<VirtualHost *:80>
     DocumentRoot /www/example1
     ServerName www.example.com
     ProxyPass /myapp ajp://localhost:8400/myapp
</VirtualHost>

Here is the configuration we used to avoid browser caching. We set the expiration time to now and the max-age=0 and the must-revalidate controls.

<VirtualHost *:80>
     DocumentRoot /www/example1
     ServerName www.example.com
     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>

     ProxyPass /myapp ajp://localhost:8400/myapp
</VirtualHost>

So above configuration will make sure that apache adds the Expires and Cache-Control to the nocache.js. This makes sure that nocache.js is always revalidated from the server.
Add also Expires to all cache files and asks browser to store them for one year.

For this we had to enable the expires module

sudo a2enmod expires

And we had to enable also the headers module

sudo a2enmod headers

And after enabling these modules, restart of apache is needed.

In the response headers of nocache.js following would be added to Cache-Control

  1. Cache-Control:
    public, max-age=0, must-revalidate

Till now no one seems to complain on redeploy. But still user needs to refresh the page in case user is working on the application while the deployment has been done. In order that the user has not to refresh while in session we started looking into Tomcat 7 Parallel Deployment .

If you want to totally avoid caching of the no.cache.js then use the below configuration in apache. This makes sure that nocache.js is always taken from the server and never cached on the browser. The difference with the above configuration is “no-store”, which asks browsers not to cache the file.

<VirtualHost *:80>
     DocumentRoot /www/example1
     ServerName www.example.com
     ExpiresActive on

     <LocationMatch "nocache">
             Header set Expires  "Tue, 03 Jul 1998 06:00:00 GMT"
             Header set Last-Modified "Tue, 03 Jul 1998 06:00:00 GMT"
             Header set Cache-Control  "no-store, no-cache, must-revalidate, max-age=0
             Header set Pragma  "no-cache"
     </LocationMatch>

     <LocationMatch ".cache.">
             ExpiresDefault "now plus 1 year"
     </LocationMatch>

     ProxyPass /myapp ajp://localhost:8400/myapp
</VirtualHost>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.