One of the great challenges in delivering content to the mobile web is that mobile devices can easily be disconnected from the Internet. As users move around, they go in and out of range of WIFI and cellular networks, and the state of their network connection can change frequently. Native applications on mobile devices solve this problem by being installed on the device. Many native applications also cache content for offline use and are designed to handle the transient nature of mobile network connections. Web browsers, including mobile web browsers, typically cache web page content to help pages load faster. While this can help mobile web applications quite a bit, the cache managed by the web browser is unreliable, can be cleared by user settings, and might not keep critical content for your application. The cache also doesn't provide a mechanism to store generated data and stores only assets required to load a web page.
In this section, we will introduce three technologies that can help you with taking a web application offline:
The HTML 5 ApplicationCache interface is designed to help address the unreliable nature of web browser caches by allowing a developer to control how content is cached for offline use. The ApplicationCache interface provides us with the following benefits:
ApplicationCache is not displaced by new content in the normal browser cache, so it is much more reliableIf you are thinking Sounds great, lets get me some of that, read on!
Taking advantage of the ApplicationCache interface is actually very simple, and there are three steps we need to take:
A MANIFEST file is a plain text file that you can create in any text editor. The easiest way to describe its content is by example, so let's take a look at a MANIFEST file and then describe it in detail:
CACHE MANIFEST # version 2 CACHE:index.htmlscripts/app.jscss/styles.cssimages/logo.png NETWORK:login.php FALLBACK:images/large/ images/offline.jpg*.html /offline.html SETTINGS:prefer-online
The first line is mandatory; it must contain the specific text CACHE MANIFEST.
The second line is a comment. Comments are any line starting with a # character. One important characteristic of ApplicationCache is that the browser will usually not replace cached content with an updated version from the server unless the MANIFEST file changes or the prefer-online setting is specified. It is a common convention to include a comment near the top of the MANIFEST file, indicating a date or version number. When content is updated, the date or version in the MANIFEST file can also be updated, which will trigger the browser to download changed content. The third line declares the CACHE section. The CACHE section explicitly declares URLs that will be stored in the ApplicationCache. This line can be optional. If it is omitted, any files after the first line will be considered part of the CACHE section up to the start of any other section. The following lines up to the NETWORK line define files, by URL, that are to be cached. Wildcards are not allowed in the CACHE section. Do not include the MANIFEST file in the list of files to be cached, or it will be very difficult for users to get updates. URLs can be relative or absolute and might point to resources on domains other than that of the page being loaded. Check the following line, for example:
CACHE: http://another.server.com/logo.png
Next, comes the NETWORK section. This section contains a whitelist of resources that the browser will not cache (unless explicitly declared in the cache section), and which it is permitted to access when online. In this example, the browser can access login.php when online, and the results of loading that page will not be cached. This section is typically used to identify URLs that are part of a server API or content that cannot be used if it is out of date. URLs cannot contain wildcards, but a single wildcard character is allowed on a line by itself to indicate that any URL can be loaded. Check the following line for example:
NETWORK: *
The FALLBACK section defines alternate resources to be used if a particular resource is not available. Wildcards and path matching are allowed in this section. The first line of the FALLBACK section shows how you would display the image from images/offline.jpg for any image in the images/large path that is unavailable. In the next line, we specify that any HTML page that cannot be accessed should use offline.html instead. URL prefixes are allowed (a path to a folder for instance), but the wildcard character * is not permitted. Also, only URLs in the same domain as the web page can be listed in this section. As of writing this book, only one value is permitted in the final SETTINGS section, the prefer-online setting. If this is present in the SETTINGS section, then the browser will attempt to access the server version of resources before using the cached version.
Note the following rules for a MANIFEST file:
CACHE MANIFEST.# character is considered a comment.# character is a valid component of a URL.CACHE section up to the start of one of the other sections. Thus the CACHE: line is not strictly needed.CACHE, NETWORK, FALLBACK, and SETTINGS. A section is started by putting the section name followed by the : character.In order to trigger the use of an ApplicationCache MANIFEST file, you must include a reference to the MANIFEST file in your web page. This is done as an attribute of the <html> tag, as follows:
<html manifest="/myapp.appcache">
The value of the attribute is the URL to your MANIFEST file. Although the MANIFEST file can have any extension, it is standard practice to use .appcache as the file extension, and we recommend you follow this practice. The MANIFEST file must be delivered by a web server with mime-type of text/cache-manifest. The instructions for configuring a web server to serve particular files with a particular mime-type vary depending on the particular web server in use. Please consult the documentation for your web server to determine the best way to accomplish this.