Gears and War

I have been doing quite a bit of Google Web Toolkit programming lately. I thought I would start sharing a few of the troubles and fixes along the way. Most recently I have been attempting to get Google Gears working for offline access with my GWT 1.7 web application.
On the surface, this isn’t too difficult. The Gears API for GWT includes a handy Offline package for automatically generate the necessary JSON manifest file to make the LocalServer happy. There are plenty examples of how to do that on the Gears website but, just in case its something like this:
private void createManagedResourceStore() {
try {
final ManagedResourceStore managedResourceStore = Offline.getManagedResourceStore();
new Timer() {
String transferringData = "Transferring data";
@Override
public void run() {
switch (managedResourceStore.getUpdateStatus()) {
case ManagedResourceStore.UPDATE_OK:
display.getOnlineStatusLabel().setText("Status: Offline");
break;
case ManagedResourceStore.UPDATE_CHECKING:
case ManagedResourceStore.UPDATE_DOWNLOADING:
transferringData += ".";
display.getOnlineStatusLabel().setText(transferringData);
schedule(500);
break;
case ManagedResourceStore.UPDATE_FAILED:
display.getOnlineStatusLabel().setText("Status: FAILED");
break;
}
}
}.schedule(500);
} catch (GearsException e) {
display.getOnlineStatusLabel().setText("");
Window.alert(e.getMessage());
}
}
That works great for everything inside your module. However, as of GWT 1.6 projects are by default using a War directory structure. This puts your default app .html/.css files (at least) one directory level higher than the module itself. Further, normally you would add any other resources to that directory as well (such as image directories and whatnot). All of those files are not gathered by the Offline automatic manifest generation. And as such, will not be caught by the Gears plugin. Further, since that html file serves your whole page….it kinda keeps your whole app from working offline. Slightly important that they be caught, in other words.
This was discussed on the google group for the Gears API, and I assume they are working on a fix. But I am by nature impatient and want it to work noooooooooow.
The easiest way I found was to let the Offline module create your manifest file, then go in and add the extra files to it that you know will need to be cached. Its a manual step, but at least it gets it working. The manifest is located in your module directory (with the other compiled output), in a file called: modulename.nocache.manifest
I just brought that sucker up in vi and added a few files to the end like so (this is only the end of the file obviously):
{ "url" : "gwt/standard/standard_rtl.css" },
{ "url" : "hosted.html" },
{ "url" : "mymodule.nocache.js" },
{ "url" : "mymodule.nocache.js?compiled" },
{ "url" : "../Mymodule.css" },
{ "url" : "../Mymodule.html" },
{ "url" : "../images/someimage.jpg" },
{ "url" : "../" }
]
}
So as you can see I just reference back one directory to pick up the other files the Offline package missed. Note I also included the base directory. This was important because I was using the greeting-file mechanism in my web.xml to by default tell the server to give me the Mymodule.html file. So you need to be sure to cache that if you want your users to be able to go to that directory without specifying the actual html file itself. Thats it! Just save that file, and clear your cache. Then when you call the above function it should correctly cache the files you added.