Using Offline Web Applications to offer offline capabilities – sister specification to HTML5
In a world where we carry our computers/Internet-connected devices with us all around, we’re bound to lose connection now and then but still want to be able to continue our work. That is where Offline Web Applications steps in.
Checking connectivity
First, let’s talk about our options to see whether a user is online or not. There are the ononline and onoffline events, that are supported by Firefox, Internet Explorer 8+ and Opera. You can check this in my Online/Offline events test page. The way to trigger the event is choosing the Work Offline mode in your web browser.
Interestingly enough, when you actually just pull out your Ethernet cable instead, IE is the only one to trigger the event then as well.
Offline Web Applications
The gist of Offline Web Applications is to provide a manifest file that states which files should be downloaded for offline usage, which fallback certain files should have and other options. It is currently supported by Firefox 3.5+, Google Chrome and Safari. Let’s look at some example code:
HTML
<!DOCTYPE html> <html manifest="offline.manifest"> <head> ...
Manifest file
CACHE MANIFEST # VERSION 10 CACHE: offline.html base.css FALLBACK: online.css offline.css
Basically, the first line declares that it is a cache manifest. Then, under CACHE, you list all the fils you want to be downloaded for offline usage (containing page with <html manifest="offline.manifest"> is automatically downloaded, but included here for clarity).
The we have the FALLBACK part where the web browser tries to access the online.css file. If it fails, it will resort to offline.css (which is also download by default since it is declared as a fallback file).
You can see this example in action in the Offline Web Applications test page, which is part of my HTML5 – Information and samples for HTML5 and related APIs test ground.
Things to think about
It is vital to know that the manifest file has to served with the text/cache-manifest content-type. Also, to take care of business for Firefox, set ExpiresByType accordingly.
Scratching the surface
This is just a mere introduction/teaser to Offline Web Applications, where you also have NETWORK options for the manifest file, and scripting possibilities through the applicationCache to update the cached files.
Gratitude
Having seen Remy Sharp (@rem) do a number of presentations on this at conferences, and having had a number of discussions and pointers with him, I really want to say thanks for the help with Offline Web Applications.

35 Comments
April 14th, 2010 at 22:00
Is IE the only one to fire such an event as onoffline because it’s the only browser with access to the OS in such a way that it would even know such a thing?
April 15th, 2010 at 11:22
mdmadph,
Perhaps. But also, there's a <code>navigator.onLine</code> property which works in IE, Firefox and Opera, which you can poll with <code>setInterval</code> or similar, and that can check if you are actually online or not in all those web browsers.
April 16th, 2010 at 15:20
Ah — aye, that would work. This is actually useful — I maintain a lot of stuff that’s used in places that still use dial-up internet.
Connections always going in and out — would be handy.
Couldn’t seem to get it to fire in WinXP-IE6, though!
April 16th, 2010 at 16:35
mdmadph,
Yes, it’s only IE8 (and later) that supports it.
April 16th, 2010 at 19:57
Crap — my fault. I meant WinXP-IE8. Did you test in Win7/Vista?
April 16th, 2010 at 22:05
mdmadph,
I've tested in Windows XP as well, through VMWare, and in there it didn't detect that I went offline, with the online/offline events, when I turned off the wireless network on the Mac. Could be due to VMWare, though.
Otherwise, polling the <code>navigator.onLine</code> worked just fine in XP too.
April 29th, 2010 at 12:52
[...] right now that needs to work on or offline (no, not for mobile, but still confined to webkit), so HTML5’s manifest attribute is the obvious choice. Unfortunately, the implementation even on Safari is spotty at best, and the [...]
April 29th, 2010 at 15:31
Thanks for this entry, Robert. Your info (which is more accurate than most) and test pages have been extremely valuable in my work the last few days.
For webkit, do you know if there are any other HTTP headers that the manifest needs than just mime type? I've copied your test onto three different servers, and it refuses to cache (with Safari 4.0.5) on just one of them. The only common HTTP header that the two have which the other does not is "Accept-Range: bytes"… which according to the HTTP 1.1 spec is already assumed.
April 29th, 2010 at 15:44
Emmet,
Glad to hear that!
Unfortunately, though, that is all I know, and I'm not sure what else can affect it.
A possible reason could be with Safari 4.05, because I have heard it has some issues. Tried Google Chrome as well?
May 5th, 2010 at 19:34
Great intro on the topic of HTML offline caching and event firing. Thanks and keep these articles coming
May 5th, 2010 at 22:30
chrelad,
Thank you!
May 24th, 2010 at 7:43
This is a nice tutorial. The next step for me where I am getting stuck is while trying to monitor the progress of downloading the files. I know the browser fires events like "downloading". But how does one actually determine how much data is downloaded and how much is still to be done?
May 24th, 2010 at 11:38
Timo,
Thanks!
One thing you could potentially do is use the XMLHTTPRequest and the progress event overall, but it's not an option for file requested via the manifest.
May 24th, 2010 at 12:00
[...] Using Offline Web Applications to offer offline capabilities [...]
June 16th, 2010 at 6:19
I have problems with passing data from one page to another when offline. It seems to be that the files in the manifest (e.g. http://foo.com) are not recognized when you add a keyvalue pair to your url (e.g. http://foo.com?key=value).
At this moment I AM THINKING OF THREE SOLUTIONS:
1. use the local db as an intermediar (not really what a db is for!!)
2. using session variables (hard to track / dirty solution)
3. single page solution using javascript injection.
The third option looks like the best one for now, but I still can not imagine that this is the best way to do it.
Is there anyone out there who has a better idea?
June 16th, 2010 at 13:37
Timo,
Interesting. I haven't played around with it much, but is a wildcard an option? As in having http://foo.com?key=* , for instance?
July 20th, 2010 at 19:04
[...] ?????????????? [...]
July 20th, 2010 at 19:15
[...] ?????????????? [...]
July 23rd, 2010 at 10:30
[...] ?????????????? [...]
July 31st, 2010 at 6:23
[...] ?????????????? [...]
August 17th, 2010 at 5:22
[...] ?????????????? [...]
November 7th, 2010 at 23:54
nice post,
i’ve been experimenting with manifest files this week and what i’ve found is Google Chrome, Safari and Opera handle them well but Firefox 3.6 doesn’t handle them so well.
haven’t tried firefox 4 with it yet but kinda stops my adoption of them.
also Google Chrome keeps returning TRUE with navigator.onLine regardless of whether your network is plugged in or not, really annoying (using Chrome 8).
November 8th, 2010 at 10:36
John,
With Firefox: did you set the ExpiresByType accordingly?
Regarding Google Chrome 8: that’s weird, and I hope it will go away before official release.
November 27th, 2010 at 6:35
when using above code its not working in Chrome
http://robertnyman.com/html5/offline/offline.html
this one is working fine
Created HTML file and Manifest file.
Is there need any changes in settings ?
I am using Google Chrome version 8.0.552.210 beta
November 27th, 2010 at 19:34
Jibu,
So, when you try the code it doesn’t work? But the demo page works for you?
In that case, have you made sure to set the ExpiresByType accordingly on your web server?
December 1st, 2010 at 13:14
Error :
Application Cache Error event: Invalid manifest mime type (text/plain)
How can i solve ?
December 1st, 2010 at 13:21
JIbu,
It needs to be served as
text/cache-manifest.For instance, add
AddType text/cache-manifest manifestto your.htaccessfile if you run Apache.March 2nd, 2011 at 6:50
[...] ?????????????? [...]
March 6th, 2011 at 10:07
[...] Using Offline Web Applications to offer offline capabilities [...]
June 20th, 2011 at 7:06
[...] ?????????????? [...]
June 22nd, 2011 at 9:39
[...] ?????????????? [...]
July 10th, 2011 at 15:12
[...] Using Offline Web Applications to offer offline capabilities [...]
August 23rd, 2011 at 9:53
[...] ?????????????? [...]
December 14th, 2011 at 4:56
[...] ?????????????? [...]
March 5th, 2012 at 2:58
[...] ?????????????? [...]
Write a comment