610 CHAPTER 16 Offline web applications
Understanding events
In general, the application cache process occurs silently, but the window.applicationCache
object enables you to inject some custom functionality into that workflow.
As the browser executes each step in the cache process, it fires a series of events on the
applicationCache object. The following is a summary of those events.
■■checking his is always the first triggered within the sequence of events. It indicates T
that the browser is checking whether the manifest file has been updated or needs to
be downloaded for the first time.
■■noupdate t’s determined that the manifest file hasn’t changed, the noupdate If i
event will be triggered. No other events will fire afterward.
■■downloading he browser determines that the file has changed or will be downIf t -
loaded for the first time, this event will be fired as the new files are being downloaded.
■■progress s the new files are being downloaded, this event will fire. It will include A
a total attribute reflecting the number of files to be downloaded. It will also include a
loaded attribute to indicate the number of files that have been downloaded thus far.
■■cached his event fires after all files have been downloaded for the first time.T
■■updateReady n existing cache has been updated, this event will fire to inform you If a
that all files have been downloaded.
■■obsolete he obsolete event will fire if the request for the manifest file results in a T
404 or 410 result. Contents of the cache will be deleted.
■■error Building the cache can fail for a number of reasons, such as an invalid resource
URL listed in the manifest. If the process fails, the error event will fire.
The applicationCache object includes an update() method that can be used to start the
caching process programmatically, as follows.
window.applicationCache.update();
After the new cache has been downloaded and the updateReady event has fired, you can
call the swapCache method to replace the old cache with the new, as follows.
window.applicationCache.addEventListener('updateready',
function(){ window.applicationCache.swapCache(); }, false );
Lesson summary
■■The application cache incorporates a manifest file that can be customized for how your
application operates when in an offline state.
■■The first line of a manifest file must contain the CACHE MANIFEST statement.
■■URLs can be explicitly included in the cache by adding them to the CACHE section of
the file, or they can be added directly underneath the CACHE MANIFEST statement.