HTML5 offline cache applicationCache directly caches remote server files through iframe. Solve the Problem of more hybrid applications

Source: Internet
Author: User

After several days of research on the offline cache of HMTL5, I checked a lot of online materials and read the books. The following is my understanding and I hope to help new users.

 1. What is Cache Manifest?

  As a web developer, the word cache is rarely heard or seen. Yes, as mentioned above, it is a cache mechanism. It can use a. manifest file to configure files that need to be cached or must be cached online. The focus is on this. manifest file. You can Google it, and I will help you sort out what you may want to know:

The server must support mime type: text/cache-manifest

NAME. manifest

2. Create the test. manifest File

CACHE MANIFEST
# Version 5.0
# Directly cached files
CACHE:
# Both absolute and relative paths are acceptable.
Js/initial. js
Js/keyevent. js
Js/uianimate. js
Js/jquery. min. js
Loading.gif
# The file to be accessed online * is a wildcard, indicating that all files not in the cache need to be accessed online.
NETWORK:
*
# Alternative solution: online access pages, offline access pages
FALLBACK
Online.html off.html

Related Events

<Script> // manually update window. applicationCache. update (); applicationCache. onchecking = function () {// check whether the manifest file exists} applicationCache. ondownloading = function () {// check whether a manifest or manifest file exists. // download the file after update. // even if the file to be cached has already been returned by the server during the request} applicationCache. onnoupdate = function (e) {// returns 304, indicating that no update is available. notifies the browser to directly use the local file} applicationCache. onprogress = function () {// triggered periodically during download. You can use it to // obtain the number of downloaded files} applicationCache. oncached = function () {// Triggered after the upload ends, indicating that the cache is successful} applicationCache. onupdateready = function () {// second load, if the manifest is updated // triggered at the end of the download // onchedalert is not triggered ("the local cache is being updated... "); If (confirm (" whether to reload updated files ") {applicationCache. swapCache (); // get the cache list of the latest version, download the resource, and update the cache to the latest location. reload () ;}/// List 5 manually update the cache // if (window. applicationCache. status = window. applicationCache. UPDATEREADY) {window. applicationCache. update ();} applicationCache. onobsolete = function () {// file not found, triggered when 404 or 401 is returned} applicationCache. onerror = function () {// other errors related to offline storage} </script>

Iii. Okay, I 've mentioned some useful and useless. Now I want to talk about the Application Scenario of mobile development. When developing the hybrid mode, the application needs to request remote resources. You need to log in and cache the resources. next time

You do not need to request the server code for the next access. when updating the code, you can apply the relevant API methods or allow the application to write the API value between two pages.

Loading.html is short for loading a simple iframe page, which can also be used for user login.

<! DOCTYPE html>
<Html>

On the index.html homepage, because the intermediate page and the offline cache have been performed, the latest code is used to determine whether the manifestfile is used to update the remote page.

<Html id = "html" manifest = 'test. manifest '> 

Summary:

When the code is cached for the first time, it must be the latest, but some updates will detect the updated resources. However, you must refresh the browser to obtain the latest code. The page in the middle is to enter the homepage.

Is to ensure that the obtained resources are up-to-date. PS: this technology is actually very depressing. It is always an inexplicable error when updating the version. If you have any friends who have used this technology, please come and talk about it, if you do not understand the situation, leave a message to me ~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.