Using the cache interface gives your application three advantages:
- Offline browsing - users can navigate your full site when they're offline. For business applications that require valid session, this scenrio could translate to caching static resources on user machines.
- Speed - cached resources are local, and therefore load faster.
- Reduced server load - the browser will only download resources from the server that have changed.
The manifest attribute should be included on every page of your web application and can point to an absolute URL or relative path to your .manifest file. The file does not need to end in ".manifest", but needs to be served with the correct mime-type (see below).
A .manifest file must be served with the mime-type text/cache-manifest. You may need to add a custom file type to your web server or .htaccess configuration.
For example, to serve this mime-type in Apache, add this line to your config file:
AddType text/cache-manifest .manifest
The cache manifest file is a simple text file that lists the resources the browser should cache for offline access and may look something like this:
CACHE MANIFEST
index.html
stylesheet.css
images/logo.png
scripts/main.js
This example will cache four files on the site. The CACHE MANIFEST string is the first line and is required. Every URL or site is limited to 5MB worth of cached data.
A manifest can have three distinct sections: CACHE, NETWORK, and FALLBACK.
CACHE:
This is the default section for entries. Files listed under this header (or immediately after the CACHE MANIFEST) will be explicitly cached after they're downloaded for the first time.
NETWORK:
Files listed under this section are white-listed resources that require a connection to the server. All requests to these resources bypass the cache, even if the user is offline.
FALLBACK:
An optional section specifying fallback pages if a resource is inaccessible. The first URI is the resource, the second is the fallback. Both URIs must be relative and from the same origin as the .manifest file
A complex example like the one below can illustrate better on how these sections work,
CACHE MANIFEST
# Explicitly cached entries
CACHE:
index.html
stylesheet.css
images/logo.png
scripts/main.js
# Resources that require the user to be online.
NETWORK:
login.jsp
/mycontroller
http*://my.domain.com
# static.html will be served if main.py is inaccessible
# offline.jpg will be served in place of all images in images/large/
FALLBACK:
/main.htm /static.html
images/large/ images/offline.jpg
Resources configured under cache manifest can be updated/purged based on events fired by appcache API such as download progress, updating the app cache, and error conditions or programatically checking for life cycle status of the appcache object.
No comments:
Post a Comment