Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML5 Application Cache

HTML5 Application Cache


With HTML5, by creating a cache manifest file, you can easily create an offline version of a web application.

Note:The manifest technology has been deprecated by web standards, and the use of this feature is no longer recommended.


What is the Application Cache?

HTML5 introduces the Application Cache, which means web applications can be cached and accessed without an Internet connection.

The Application Cache brings three advantages to applications:

  1. Offline browsing - users can use the application when it is offline
  2. Speed - cached resources load faster
  3. Reduced server load - the browser will only download updated or changed resources from the server.

Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

Internet Explorer 10, Firefox, Chrome, Safari, and Opera support the Application Cache.


HTML5 Cache Manifest Example

The following example shows an HTML document with a cache manifest (for offline browsing):

Example

.appcache

">

...

Every page that specifies a manifest is cached when the user visits it. If the manifest attribute is not specified, the page will not be cached (unless the page is specified directly in the manifest file).

The recommended file extension for a manifest file is: ".appcache".

Remark Please note that the manifest file needs to be configured with the correct MIME-type, namely "text/cache-manifest". This must be configured on the web server.


The Manifest File

The manifest file is a simple text file that tells the browser what to cache (and what not to cache).

The manifest file can be divided into three sections:

CACHE MANIFEST

The first line, CACHE MANIFEST, is required:

CACHE MANIFEST

/theme.css

/logo.gif

/main.js

The manifest file above lists three resources: a CSS file, a GIF image, and a JavaScript file. When the manifest file is loaded, the browser downloads these three files from the root directory of the website. Then, no matter when the user disconnects from the Internet, these resources remain available.

NETWORK

The following NETWORK section specifies that the file "login.php" is never cached and is unavailable when offline:

NETWORK:

login.php

An asterisk can be used to indicate that all other resources/files require an Internet connection:

NETWORK:

FALLBACK

The following FALLBACK section specifies that if an Internet connection cannot be established, "offline.html" will replace all files in the /html5/ directory:

FALLBACK:

/html/ /offline.html

Note: The first URI is the resource, and the second is the fallback.


Updating the Cache

Once an application is cached, it remains cached until the following occurs:

Example - Complete Manifest File

CACHE MANIFEST

2012-02-21 v1.0.0

/theme.css

/logo.gif

/main.js

NETWORK:

login.php

FALLBACK:

/html/ /offline.html

Remark**Tip:** Lines beginning with "#" are comments, but can also serve other purposes. The application's cache is updated when its manifest file changes. If you edit an image, or modify a JavaScript function, those changes will not be re-cached. Updating the date and version number in a comment line is a way to make the browser re-cache the files.


Notes on the Application Cache

Please pay attention to what is cached.

Once a file is cached, the browser continues to display the cached version, even if you modify the file on the server. To ensure that the browser updates the cache, you need to update the manifest file.

Note: Browsers may have different capacity limits for cached data (some browsers set a limit of 5MB per site).