GWT Caching Concept

12,365

Solution 1

Generally, there are 3 type of files -

  1. Cache Forever
  2. Cache for some time
  3. Never Cache

Some files can never be cached, and will always fall in the "never cache" bucket. But the biggest performance wins comes from systematically converting files in the second bucket to files that can be cached forever. GWT makes it easy to do this in various ways.

The <md5>.cache.js files are safe to cache forever. If they ever change, GWT will rename the file, and so the browser will be forced to download it again.

The .nocache.js file should never be cached. This file is modified even if you change a single line of code and recompile. The nocache.js contains the links of the <md5>.cache.js, and therefore it is important that the browser always has the latest version of this file.

The third bucket contains images, css and any other static resources that are part of your application. CSS files are always changing, so you cannot tell the browser 'cache forever'. But if you use ClientBundle / CssResource, GWT will manage the file for you. Every time you change the CSS, GWT will rename the file, and therefore the browser will be forced to download it again. This lets you set strong cache headers to get the best performance.

In summary -

  1. For anything that matches .cache., set a far-in-the-future expires header, effectively telling the browser to cache it forever.
  2. For anything that matches .nocache., set cache headers that force the browser to re-validate the resource with the server.
  3. For everything else, you should set a short expires header depending on how often you change resources.
  4. Try to use ClientBundle / CssResource; this automatically renames your resources to *.cache bucket

Solution 2

This blog post has a good overview of the GWT bootstrapping process (and many other parts of the GWT system, incidentally), which has a lot to do with what gets cached and why.

Basically, the generated nocache.js file is a relatively small bit of JS whose sole purpose is to decide which generated permutation should be downloded.

Each individual permutation consists of the implementation of your app specific to the browser, language, etc., of the user. This is a lot more code than the simple bootstrapping code, and thus needs to be cached for your app to respond quickly. These are the cache.html files that get generated by the GWT compiler.

When you recompile and deploy your app, your users will download the nocache.js file as normal, but this will tell their browsers to download a new cache.html file with the app's new features. This will now be cached as well for the next time they load your app.

Share:
12,365
Noor
Author by

Noor

Love programming, designing system, their architectures, fond of client side web programming using javascript frameworks such as angularjs, emberjs or other like GWT

Updated on June 04, 2022

Comments

  • Noor
    Noor almost 2 years

    Can someone explain to me in simple term the concept of caching in GWT. I have read this in many places but may be due to my limited knowledge, i'm not being able to understand it.

    Such as nocache.js, cache.js

    or other things such as making the client cache files forever or how to make files cached by the client and then if file get changed on the server only then the client download these files again

  • David Mann
    David Mann about 10 years
    My GWT 2.4 applications create <md5>.cache.html files, instead of <md5>.cache.js.
  • GreenKiwi
    GreenKiwi over 9 years
    Oddly, I have two different GWT applications, one generates cache.html, the other generates cache.js