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.
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? π
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.
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!
mdmadph,
Yes, it’s only IE8 (and later) that supports it.
Crap — my fault. I meant WinXP-IE8. Did you test in Win7/Vista?
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.
[…] 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 […]
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.
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?
Great intro on the topic of HTML offline caching and event firing. Thanks and keep these articles coming π
chrelad,
Thank you! π
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?
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.
[…] Using Offline Web Applications to offer offline capabilities […]
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?
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?
[…] ?????????????? […]
[…] ?????????????? […]
[…] ?????????????? […]
[…] ?????????????? […]
[…] ?????????????? […]
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).
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.
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
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?
Error :
Application Cache Error event: Invalid manifest mime type (text/plain)
How can i solve ?
JIbu,
It needs to be served as
text/cache-manifest
.For instance, add
AddType text/cache-manifest manifest
to your.htaccess
file if you run Apache.[…] ?????????????? […]
[…] Using Offline Web Applications to offer offline capabilities […]
[…] ?????????????? […]
[…] ?????????????? […]
[…] Using Offline Web Applications to offer offline capabilities […]
[…] ?????????????? […]
[…] ?????????????? […]
[…] ?????????????? […]