Cache manifest in HTML5

Filename extension .appcache
Internet media type text/cache-manifest
Developed by World Wide Web Consortium
Standard HTML5
Open format? Yes
Website www.whatwg.org/specs/web-apps/current-work/multipage/offline.html

The cache manifest in HTML5 is a software storage feature which provides the ability to access a web application even without a network connection. It became part of the W3C Recommendation on 28 October 2014. [1] According to HTML Living Standard it will be replaced. [2]


Background

Web applications consist of web pages that need to be downloaded from a network. For this to happen there must be a network connection. However, there are many instances when users cannot connect to a network due to circumstances beyond their control. HTML5 provides the ability to access the web application even without a network connection using the cache manifest.

Web applications consist of resources identified by URLs. These can be HTML, CSS, JavaScript, images or any other source that is required for a web application to be rendered. Their addresses can be copied into a manifest file, which can be updated regularly by the author of the web application, indicating any new web addresses that are added or deleted. When connecting to a network for the first time, a web browser will read the HTML5 manifest file, download the resources given and store them locally. Then, in the absence of a network connection, the web browser will shift to the local copies instead and render the web application offline.

Basics

In order for the offline applications to work, a cache manifest file must be created by the web developer. If the web application exceeds more than one page then each page must have a manifest attribute that points to the cache manifest. Every page referencing the manifest will be stored locally.[3] The cache manifest file is a text file located in another part of the server. It must be served with the following content type:[4]

text/cache-manifest

The following line must be added to the html element in order for the cache manifest file to work.[4]

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

The argument to the manifest attribute is a relative or absolute path to the manifest file.

Consider the HTML file given below. The <html> element indicates a file named cache.appcache will contain the list of resources (i.e., test.js, test.css) needed for this web page to work offline. Common names for this file are cache.manifest and manifest.appcache.

<!—- test.html -->
<!DOCTYPE HTML>
<html manifest="cache.appcache">
<head>
  <title>Test</title>
  <script src="test.js"></script>
  <link rel="stylesheet" href="test.css">
</head>
<body>
  Testing the manifest file.
</body>
</html>

Syntax

Given below is a list of some rules and syntax required when writing the manifest file.[5]

CACHE MANIFEST 
# the above is a required line
# this is a comment 
# spaces are ignored
# blank lines are ignored

Given below is an example of a cache manifest file.

Example 1:

CACHE MANIFEST 
/test.css
/test.js
/test.png

This manifest file lists three resources: a CSS file, a JavaScript file and a PNG image. When the above file is loaded, the browser will download the test.css, test.js and test.png files from the root directory in the web server.[4] As a result, whenever one's network is not connected, the resources will be available to them offline.

Cache manifests can also use relative paths or even absolute URLs as shown below.[5][6][7]

Example 2:

CACHE MANIFEST
/main/features.js
/main/settings/index.css
http://files/images/scene.jpg
http://files/images/world.jpg

File headers

The cache manifest file consists of three section headers.[4]

  1. Explicit section with the header CACHE.
  2. Online whitelist section with the header NETWORK.
  3. Fallback section with the header FALLBACK.

Note: Example 1 and Example 2 above, do not indicate any section header and are therefore considered an explicit section by default.

Online whitelist section with the header NETWORK

Example 3:

CACHE MANIFEST 
NETWORK: 
/checking.cgi
CACHE:
/test.css
/test.js
/test.png

This example consists of headers. The line, NETWORK: is the start of the "online whitelist" section. The resources listed under this section are never cached and are not available offline.[4] As a result, an error will occur when an attempt is made offline to load the resource.

There is a shift to the explicit section by the header CACHE: and the resources (the CSS stylesheet, JavaScript and the image file) can be downloaded and used offline.

Fallback section with the header FALLBACK

The fallback section in a cache manifest file can be used to substitute online resources that cannot be cached or were not cached successfully.[4]

Example 4:

CACHE MANIFEST
FALLBACK:
/ /offline.html 
NETWORK:
…

In Example 4, the fallback section consists of a single line. i.e., / /offline.html. The single character (/) before ‘offline’ will match any URL pattern on one's site.[4] If the browser does not find the page in the appcache, the application will display the page /offline.html.

Event flow

Events are under the ApplicationCache JavaScript object.

If the browser visits a web page, has NOT seen the web page before and as a result does not recognize the manifest file, the following events will ensue.[5]

If the browser has visited the web page before and recognizes the manifest file the following events will ensue.[5]

If an error occurs at any instance in the above events, the browser will trigger an error event and stop the process. Given below are a few errors that can occur when re-downloading resources.[6]

See also

References

  1. "Application cache as part of the W3C Recommendation". 28 October 2014. Retrieved 30 May 2016.
  2. "Offline Web applications is in the process of being removed from the Web platform". 28 May 2016. Retrieved 30 May 2016.
  3. Bidelman, Eric (29 October 2013). "A Beginner's Guide to Using the Application Cache". Retrieved 23 April 2014.
  4. 1 2 3 4 5 6 7 8 9 10 Pilgrim, Mark (2010). HTML5 Up and Running. O'Reilley.
  5. 1 2 3 4 "W3 HTML5 Manifests". HTML5. Retrieved 3 April 2011.
  6. 1 2 "Dev.Opera". HTML5. Retrieved 3 April 2011.
  7. "WHATWG". HTML5. Retrieved 3 April 2011.

External links

This article is issued from Wikipedia - version of the 5/30/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.