HTML5 Application Cache

The HTML5 Application Cache API provides a mechanism for caching the files and resources used by a web page. Once cached, it's as if the user downloaded and installed your application on their device. This allows the application to be used offline when the user is not connected to the Internet.

Note

Browsers may limit the amount of data that can be cached. Some browsers limit it to 5 MB.

The key to getting your application cached is the cache manifest file. This file is a simple text file that contains information about what resources should be cached. It is referenced by the manifest attribute on the <html> element of your web page:

<html manifest="myapp.appcache">

Inside the manifest file, you can specify the resources to cache or not cache. The file can have three sections:

  • CACHE: This is the default section and lists the files to be cached. Declaring this section header is optional. Wildcards are not allowed in URIs.
  • NETWORK: This section lists the files that require a network connection. Requests for these files bypass the cache. Wildcards are allowed.
  • FALLBACK: This section lists fallback files if a resource is not available offline. Each entry contains the URI of the original file and the URI of the fallback file. Wildcards are allowed. Both URIs must be relative and from the same domain as the application.

Note

The cache manifest file can have any file extension, but it must be delivered with a MIME type of text/cache-manifest. You may need to associate the extension you use with this MIME type in your web server.

One important thing to note is that once the files for an application are cached only those versions of the files will be used, even if they change on the server. There are only two ways that the resources in the application cache can be updated:

  • When the manifest file changes
  • When the user clears the browser's data storage for your application

I recommend keeping the cache manifest file out of the same folder as your HTML file while developing your application. You don't want files cached while you are writing code. Put it in the base folder of your application along with your release script and copy it to the release folder in your script.

Whether you cache your application or not depends on the nature of your application. If it heavily depends upon Ajax calls to the server to work then making it available offline would be pointless. However, if you can write your application so that it stores data locally while offline, then it might be worthwhile. You should determine if the overhead of maintaining a cache manifest provides a benefit to your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.1.239