Jump to content

Cache manifest in HTML5

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Achinikh (talk | contribs) at 05:13, 16 April 2011 (Created page with 'Web applications consist of web pages that need to be downloaded from a network. For this to happen there must be a network connection. There are many instances...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Web applications consist of web pages that need to be downloaded from a network. For this to happen there must be a network connection. There are many instances when we cannot connect to a network due to reasons beyond our control. HTML5 provides the ability to access the web application even without a network connection.

A web application consists of a list of web addresses. These can be HTML, CSS, JavaScript, images or any other source that is required for the web application to be rendered. These addresses or URL's can be copied onto 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.

The Cache Manifest In HTML5

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.[1]The cache manifest file is a text file located in another part of the server.It must be served with the following content type:[1]

                         text/cache-manifest

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


                         <!DOCTYPE HTML>
                         <html manifest=”/cache.manifest”>
                           <body>
                              …
                           </body>
                         </html>


Consider the HTML file given below. The <html> element indicates the test.manifest file. This manifest will contain the list of resources (i.e. test.js, test.css) needed for this web page to work offline.

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


The Cache Manifest Syntax

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

                          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.jpg


This manifest file lists three resources: a CSS file, a JavaScript file and a JPEG image. When the above file is loaded, the browser will download the test.css, test.js and test.jpg files from the root directory in the web server.[1] As a result, whenever your network is not connected, the resources will be available to you offline.

Cache manifests can also use absolute paths or even absolute URLs as shown below.[2] [3][4]

Example 2:

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


Cache Manifest File Headers

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

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

Explicit section with the header CACHE

Example 1 and Example 2 above, does not indicate any section header and is 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.jpg


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.[1] 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.[1]

Example 4:

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


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


The Flow of Events

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.[2]

  • Checking Event - occurs when the browser visits a web page and reads the manifest attribute on the <html> element.
  • Downloading Event - if the browser has never come across this manifest file before, it will download all the resources given in the manifest file.
  • Progress Event - contains information of how many files have been downloaded and how many files are left to be downloaded.
  • Cached Event - occurs once all the files have been downloaded and the offline web application is equipped to be used offline.


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

  • Noupdate Event - this will take place if the cache manifest has not changed.
  • Downloading Event - if the cache manifest has changed the resources the file will be downloaded again.
  • Progress Event - this contains information of how many files have been downloaded and how many files are left to be downloaded.
  • Updateready Event - after the re-downloading is complete, this event is triggered, indicating that the new offline version is ready to be used.


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.[3]

  • Page Not Found (HTTP error 404) or Page Permanently Gone (HTTP error 410).[5]
  • Failure to download the HTML page that pointed to the manifest.[1]
  • The cache manifest changed while the update occurred.[1]
  • The cache manifest was changed but the browser did not download a resource in the manifest.[1]


See also

References

1. "HTML5 Up and Running" Mark Pilgrim. O'Reilly/Google Press. August 2010 [1]

2. "W3 HTML5 Manifests". [2]

3. "Dev.Opera". [3]

4. "WHATWG" [4]

5. "HTTP Status Codes" [5]

  1. ^ a b c d e f g h i j k Cite error: The named reference HTML5 Up and Running was invoked but never defined (see the help page).
  2. ^ a b c d Cite error: The named reference W3 HTML5 Manifests was invoked but never defined (see the help page).
  3. ^ a b c Cite error: The named reference Dev.Opera was invoked but never defined (see the help page).
  4. ^ Cite error: The named reference WHATWG was invoked but never defined (see the help page).
  5. ^ Cite error: The named reference HTTP Status Codes was invoked but never defined (see the help page).