Application Cache, introduced in HTML5, enables web-based applications to function offline by automatically saving the HTML file and all associated resources on the user's local machine. This means that even without an internet connection, the browser can still access the web page and its resources.
Here are some key advantages of using HTML5 application cache:
Tip: Application Cache is supported in all major modern web browsers, including Firefox, Chrome, Opera, Safari, and Internet Explorer 10 and above.
To enable caching of files for offline use, follow these steps:
A manifest is a special text file that instructs the browser on which files to cache, which to exclude, and what to do in case of network failures. The manifest file always begins with the words CACHE MANIFEST
. Here's a basic example:
CACHE MANIFEST
# v1.0 : 10-08-2014
CACHE:
# pages
index.html
# styles & scripts
css/theme.css
js/jquery.min.js
js/default.js
# images
/favicon.ico
images/logo.png
NETWORK:
login.php
FALLBACK:
/ /offline.html
Let's dive into what this code means. A manifest file has three main sections: CACHE, NETWORK, and FALLBACK.
Note: When an application cache exists, the browser loads documents and resources directly from it, bypassing the network. If the manifest file is updated on the server, the browser downloads the new version and the resources it lists.
Warning: Avoid specifying the manifest file itself in the cache manifest, as it can make it difficult for the browser to know when a new manifest is available.
Once you've created it, upload your cache manifest file to the web server. Ensure the server is set up to serve manifest files with the MIME type text/cache-manifest.
To activate your cache manifest, add the manifest attribute to the root <html>
element in your web pages:
<!DOCTYPE html>
<html lang="en" manifest="example.appcache">
<head>
<title>Using the Application Cache</title>
</head>
<body>
<!--The document content will be inserted here-->
</body>
</html>
Note: If you're using Apache web servers, you can configure the MIME type for manifest files (.appcache) by adding AddType text/cache-manifest .appcache
to a .htaccess
file located in either the root directory of your application or the same directory as the application itself.