Simple Offline Application

I’ve written my fair share of single file applications. All the JavaScript and CSS are inline, and if I’m feeling particularly ninja, I’ll base64 encode the images, and make them inline too.

To make the whole thing completely available offline is insanely easy, and reusable to boot.

The first step is to add the manifest attribute to the html element:

<html manifest="self.manifest">

In this case we’ve got a file called self.manifest which is pretty simple. It contains the following:

CACHE MANIFEST

Yep, that’s it. Since the application cache automatically includes the file that references the manifest file, we’ve now got an offline application cache for our single file app.

Make sure you’re serving the manifest file correctly, if you’re not sure, check out the HTML5 Doctor introduction to offline applications, but otherwise, that’s it.

When I went away this last week, I wrote a little Boggle clone, and wanted it work offline on the plane – so I added this technique to the single file app (it’s only the board and a countdown, not interactive).

Dirt simple, but totally reusable!

Note: the manifest file is subject to the same-origin rule, in that you can’t point the manifest attribute to something like http://simpleapp.com/self.manifest. Equally, you can’t base64 encode the manifest file as some people have asked in the comments – that would be really awesome!

11 Responses to “Simple Offline Application”

  1. Remy…

    For something as simple as this Boggle board, what’s the benefit of making it “offline” vs just running the code in localhost?

  2. Can’t you b64-inline that manifest file, too?

  3. Deviously simple!

  4. What would base64′n CACHE MANIFEST do?

    so:

    <html manifest="data:text/cache-manifest;base64=Q0FDSEUgTUFOSUZFU1Q=">

  5. @Andy – none if you’re just running from your localhost. However, if you wanted to share it and make the single file app offline it’s easy. There’s also the simple fact that I couldn’t run this simple boggle page from a localhost on my iPad! :)

    @Jos – I tried that, although I knew the answer, but to confirm it: no – you can’t base64 encode it. Shame, because that would be cool.

  6. Allowing an inline manifest would be a security flaw, imho.

  7. @David – well, if anything, it would be impossible to update the application, since it would be cached and there’d be no way to update the manifest (through an update application.html file).

  8. Now that base64 is nixed, I wonder about constructing a manifest with the File API.

    … well you could construct the file but I suppose you end up with nothing if you can’t base64 reference its value.. Nevermind!

  9. I apologyze in advance because I’m really new to html5 (i’m still reading the book) but what is all the fuzz for getting the manifest encoded with base64??

    Why is that?

  10. @Scyfox If I understand correctly, the idea of using base64 is to embed as much of the application assets into the same html page as possible.

    You can, for example embed an image directly:

    <img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav..ETC=" alt="embedded image" width="80" height="15" />

    This way, you don’t need to include the image as a separate file and you don’t need to maintain the proper hyperlinks to it.

    This is not a HTML5 feature, but rather part of the previous HTML 4.01 standard. More info available here: http://en.wikipedia.org/wiki/Data_URI_scheme

    Hope this clarifies things for you.

  11. HTML5 introduced offline application, but browsers are still very old (read IE 6/7) to adopt it. In-fact, those old browsers should be absolute by now, but to our luck, are still being used.

    BTW, nice article.

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight