HTML5 Application Cache

HTML5 Tutorial

HTML5 Application Cache

Completed

Using the application cache it can be very easy to create an offline version of a web application, by creation of a cache manifest file.

What is Application Cache?

Completed

HTML5 has introduced application cache, which enables a web application to be cached, and accessible without having an internet connection.

Application cache can giva an application these three advantages:

  1. Offline browsing - users have the option to use the application when they're not having internet access or offline
  2. Speed - cached resources will get loaded faster
  3. Reduction of server load - the browser will be only downloading updated/changed resources in the webpage from the server

Browser Support

Completed

The table numbers specify the version of thefirst browser which was completely supporting Application Cache.

API
Application Cache 4.0 10.0 3.5 4.0 11.5

HTML Cache Manifest Example

Completed

The example below illustrates how an HTML document with a cache will manifest (for offline browsing):

Example

<!DOCTYPE HTML>
<html manifest="demo.appcache">

<body>
The content of the document......
</body>

</html>
Try it Yourself

Cache Manifest Basics

Completed

Inorder to enable application cache, inclusion of the manifest attribute in the document's <html> tag:

<!DOCTYPE HTML>
<html manifest="demo.appcache">
...
</html>

Every page having the manifest attribute specified will undergo cached when the user is visiting it. Incase the manifest attribute is not specified, the page will not undergo cache (unless and until the page gets specified inside the manifest file directly).

The file extension that is recommended for manifest files is: ".appcache"

The Manifest File

Completed

The manifest file can be known as a simple text file, which instructs the browser what to cache (and what not to cache).

The manifest file consists of three sections:

  • CACHE MANIFEST - Files listed with this header will be cached after their first time download
  • NETWORK - Files listed with this header will be requiring a connection to the server, and will not be cached at all
  • FALLBACK - Files listed with this header will specify fallback pages incase a page gets inaccessible

CACHE MANIFEST

The first line - CACHE MANIFEST, is needed:

CACHE MANIFEST
/theme.css
/logo.gif
/main.js

The manifest file listed above consists of three resources: a CSS file, a GIF image, and a JavaScript file. As soon as the manifest file is loaded, the browser will be downloading the three files from the web site's root directory. So, even if the user is not having a connection to the internet, the resources is still available.

NETWORK

The NETWORK section as shown below illustrates that the file "login.asp" should not be cached, and is not available offline too:

NETWORK:
login.asp

An asterisk is used to denote that all other resources/files will be requiring an internet connection:

NETWORK:
*

FALLBACK

The FALLBACK section below is specifying that "offline.html" will be availalbe as a replacement of all files in the /html/ catalog, if internet connection is lost:

FALLBACK:
/html/ /offline.html

Note: The first URL is known as the resource, the second is known as the fallback

Updating the Cache

Completed

If an application is cached, it will remain to be cached till any one of the following is happening:

  • The user is clearing the browser's cache
  • The manifest file is getting modified (see tip below)
  • The application cache is updated using a program

Example - Complete Cache Manifest File

CACHE MANIFEST
# 2012-02-21 v1.0.0
/theme.css
/logo.gif
/main.js

NETWORK:
login.asp

FALLBACK:
/html/ /offline.html

Notes on Application Cache

Completed

You need to be careful while deciding what you cache.

If a file is cached, the browser will be showing the cached version, even if you are changing the file on the server. To make sure the browser is updating the cache, you must be changing the manifest file.

Note: Different browsers will be having different size limits for these cached data (some of the browsers have for eg - a 5MB limit per site).