API: Script: Appcache Handler

Trott edited this page Feb 28, 2012 · 11 revisions
Clone this wiki locally

For the impatient: Go directly to How do I use the Appcache Handler? at the end of this page.

About Appcache

If you are unfamiliar with HTML5 offline application caching (Appcache), there are several excellent resources available online. For example, perhaps you might enjoy reading Dive Into HTML5: Let's Take This Offline.

If you need a tool to assist you in determining what your offline caching manifest should contain, perhaps you will find the Manifestr bookmarklet helpful.

If you need to have content available to users of your site even when they have no network connectivity, HTML5 offline application caching can do that.

In addition, you can use an Appcache to improve performance. This is especially useful with mobile devices that often have relatively slow and unreliable network connections.

Why do I need an Appcache Handler?

The MWF Device Telemetry Stack (DTS) complicates HTML5 offline application caching (Appcache). Fortunately, the MWF Appcache Handler makes it easy again.

The DTS will sometimes need the MWF JavaScript handler to reload the page. This is by design. On page reload, the JavaScript changes and all proceeds as planned. However, if the JavaScript that causes a reload is placed in the HTML5 offline appcache, the browser will reload again and again.

Choosing to not place the JavaScript handler into the Appcache is not a good solution. The handler is essential to the more advanced features of MWF. So you will want the JavaScript to be in the Appcache so users will have access to those features when they are offline.

Enter the Appcache Handler, which knows when to cache and when not to cache things.

How do I use the Appcache Handler?

  1. Create your manifest as usual in root/assets/appcache/manifest.appcache.
  2. You can enable the Appcache on the appropriate page (usually root/index.php if you intend to cache the landing page for your MWF site) via the add_appcache() method of the HTML Decorator:

    echo HTML_Decorator::html_start()->add_appcache()->render();

If you are not using the MWF Decorator classes, you can specify the Appcache Handler in your <html> tag:

<html manifest="http://www.example.com/assets/appcache.php">

And that's it! Your content will only be placed in the HTML5 offline application cache when it is safe for the browser to do so.