search
HomeWeb Front-endH5 TutorialWhat is HTML5 offline cache Manifest_html5 tutorial skills

Web apps are no better than PCs. There are performance and traffic considerations. Offline applications are becoming more and more important. Although browsers have caching mechanisms, they are often unreliable. What’s more, HTML files cannot be cached under normal circumstances. Everything is over after the Internet.

What is manifest?

To put it simply, manifest allows your application to be accessed even without a network connection.

It has three major advantages :

1. Offline browsing, normal access even without network;

2. Faster loading speed, cached local access speed is naturally faster;

3. Reduce the service request pressure. After the file is cached, there is no need to request it again, only the files that need to be updated.

How to use?

XML/HTML CodeCopy content to clipboard
  1. /span>>
  2. html manifest="demo. appcache">
  3. ...
  4. html>

You need to include the manifest attribute on every page of the web app you want to cache. If a page does not have a manifest attribute, it will not be cached (unless the page is explicitly specified in the manifest file). This means that as long as the page visited by the user contains the manifest attribute, it will be added to the application cache. In this way, there is no need to specify which pages need to be cached in the manifest file.

The Manifest attribute can specify an absolute URL or a relative path. However, an absolute URL needs to be from the same origin as the web app. A manifest file can be any extension file type, but it must have the correct mime-type, such as adding

in Apache

"AddType text/cache-manifest .appcache".

Manifest file

Manifest files are simple text files that tell the browser what is cached (and what is not cached).

Manifest files can be divided into three parts:

CACHE MANIFEST - Files listed under this heading will be cached after the first download
NETWORK - in The files listed under this heading require a connection to the server and are not cached
FALLBACK - The files listed under this heading specify a fallback when the page is unreachable Page (such as 404 page)

A complete manifest file:

XML/HTML CodeCopy content to clipboard
  1. CACHE MANIFEST
  2. # 2012-02-21 v1.0.0
  3. /theme.css
  4. /logo.gif
  5. /main.js
  6. NETWORK:
  7. login.asp
  8. FALLBACK:
  9. /html5/ /404.html

CACHE MANIFEST is required. # is followed by a comment. Below are the files that need to be cached and require a relative path. NETWORK is a file that needs to be loaded with each request.
An asterisk can be used to indicate that all other resources/files require an Internet connection:
NETWORK:
*
FALLBACK is if an Internet connection cannot be established , then use "404.html" to replace all files in the /html5/ directory.

Update mechanism
There are three ways to update the manifest cache:
1. The user clears the browser cache;
2. The manifest file is modified, even if it is a comment (so it can be modified by Comment to update the file)
3. Update by program

Cache status
You can view the cache status in the program through the window.applicationCache property.

C/C CodeCopy content to clipboard
  1. var appCache = window.applicationCache;   
  2.     
  3. switch (appCache.status) {   
  4.     
  5.   case appCache.UNCACHED: // UNCACHED == 0   
  6.     
  7.     return ‘UNCACHED’;   
  8.     
  9.     break;   
  10.     
  11.   case appCache.IDLE: // IDLE == 1   
  12.     
  13.     return ‘IDLE’;   
  14.     
  15.     break;   
  16.     
  17.   case appCache.CHECKING: // CHECKING == 2   
  18.     
  19.     return ‘CHECKING’;   
  20.     
  21.     break;   
  22.     
  23.   case appCache.DOWNLOADING: // DOWNLOADING == 3   
  24.     
  25.     return ‘DOWNLOADING’;   
  26.     
  27.     break;   
  28.     
  29.   case appCache.UPDATEREADY:  // UPDATEREADY == 4   
  30.     
  31.     return ‘UPDATEREADY’;   
  32.     
  33.     break;   
  34.     
  35.   case appCache.OBSOLETE: // OBSOLETE == 5   
  36.     
  37.     return ‘OBSOLETE’;   
  38.     
  39.     break;   
  40.     
  41.   default:   
  42.     
  43.     return ‘UKNOWN CACHE STATUS’;   
  44.     
  45.     break;   
  46.     
  47. };   
  48.   

To update the cache programmatically, first call applicationCache.update(). This will attempt to update the user's cache (requiring the manifest file has changed). Finally, when applicationCache.status is in the UPDATEREADY state, call applicationCache.swapCache() and the old cache will be replaced with the new one.

C/C CodeCopy content to clipboard
  1. var appCache = window.applicationCache;
  2. appCache.update(); // Attempt to update the user’s cache.
  3. if (appCache.status == window.applicationCache.UPDATEREADY) {
  4. appCache.swapCache(); // The fetch was successful, swap in the new cache.
  5. }

Note: Using update() and swapCache() like this will not present the updated resource to the user. This simply lets the browser check whether the manifest file has been updated, then download the specified update content and repopulate the app cache. Therefore, for users to see the updated content, two page downloads are required, one to update the app cache and one to update the page content.

To allow users to see the latest version of your site, set up a listener to listen for the updateready event when the page loads.

C/C CodeCopy content to clipboard
  1. // Check if a new cache is available on page load.
  2. window.addEventListener(‘load’, function(e) {
  3. window.applicationCache.addEventListener(‘updateready’, function(e) {
  4. if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
  5.  // Browser downloaded a new app cache.
  6.  // Swap it in and reload the page to get the new hotness.
  7. window.applicationCache.swapCache();
  8. window.location.reload();
  9. } else {
  10.  // Manifest didn’t changed. Nothing new to server.
  11. }
  12. }, false);
  13. }, false);

Listen to events and handle different states accordingly:

C/C CodeCopy content to clipboard
  1. var appCache = window.applicationCache;
  2. // Fired after the first cache of the manifest.
  3. appCache.addEventListener(‘cached’, handleCacheEvent, false);
  4. // Checking for an update. Always the first event fired in the sequence.
  5. appCache.addEventListener(‘checking’, handleCacheEvent, false);
  6. // An update was found. The browser is fetching resources.
  7. appCache.addEventListener(‘downloading’, handleCacheEvent, false);
  8. // The manifest returns 404 or 410, the download failed,
  9. // or the manifest changed while the download was in progress.
  10. appCache.addEventListener(‘error’, handleCacheError, false);
  11. // Fired after the first download of the manifest.
  12. appCache.addEventListener(‘noupdate’, handleCacheEvent, false);
  13. // Fired if the manifest file returns a 404 or 410.
  14. // This results in the application cache being deleted.
  15. appCache.addEventListener(‘obsolete’, handleCacheEvent, false);
  16. // Fired for each resource listed in the manifest as it is being fetched.
  17. appCache.addEventListener(‘progress’, handleCacheEvent, false);
  18. // Fired when the manifest resources have been newly redownloaded.
  19. appCache.addEventListener(‘updateready’, handleCacheEvent, false);

If the manifest file or a resource specified in the file fails to download, the entire update will fail. In this case, the browser will continue to try the old application cache.

Note:

1. The capacity limit of the site's offline storage is 5M
2. If the manifest file or a file listed internally cannot be downloaded normally, the entire update process will be regarded as a failure, and the browser will continue to use the old cache
3. The html that references the manifest must have the same origin as the manifest file and be in the same domain
4. The relative path used in the manifest, the relative reference is the manifest file
5. The CACHE MANIFEST string should be in The first line, and essential
6. The system will automatically cache the HTML file that references the manifest file
7. The CACHE in the manifest file has nothing to do with the position order of NETWORK and FALLBACK. If it is an implicit declaration, it needs to be in Front
8. The resources in FALLBACK must have the same origin as the manifest file
9. When a resource is cached, the browser directly requests the absolute path and will also access the resource in the cache.
10. Even if the manifest attribute is not set for other pages in the site, the requested resource will be accessed from the cache if it is in the cache
11. When the manifest file changes, the resource request itself will trigger an update

The above is an introduction to the relevant content of HTML5 offline caching Manifest. I hope it will be helpful to everyone's learning.

Original text: http://www.cnblogs.com/hutuzhu/p/4871666.html

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool