Foreword
Today's Web applications are already very complex. With current development, they will become more and more complex. However, they have a fatal flaw, which is that they cannot be disconnected from the Internet. Therefore, a new API has been added to HTML,
It uses a local storage mechanism to solve this problem well, paving the way for offline web applications.
Local cache in browser cache
Any webpage has a webpage cache
The local cache only caches those pages that you specify to cache
Web page caching is unreliable and unsafe because we don’t know which pages and resources are cached in the website
Local caching can control what content is cached
The local cache of web applications is managed through the manifest file of each page. The manifest is a simple text in which the names and paths of files that need to be cached and do not need to be cached are listed in the form of a list. .
The manifest can be specified individually for each page or for the entire application. For example, our settings for hello.htm:
When specifying file source files, resource files can be divided into three categories, CACHE, NETWORK, and FALLBACK
Specify resource files that need to be cached locally in the CACHE category. When specifying resource files that need to be cached locally for a certain page, you do not need to specify the page itself in the CACHE category,
because if a page With a manifest file, the browser will automatically cache the page locally
The NETWORK category is resource files that are explicitly specified not to be cached. These files can only be accessed by establishing a server-side link. In this example, the wildcard * is used to indicate that those that are not recorded will not be cached
Each line in the FALLBACK category specifies two resource files. The first resource file is the resource file used when it can be accessed online, and the second is the local cache file used when it cannot be accessed online
When working with offline web applications, it is necessary to understand the interaction process between the browser and the server:
Open the URL again
Request url
The browser finds that the page is cached, so it uses the local cache file
to parse the file
The browser requests the manifest file from the server
and the server returns 304, Notify that the manifest file has not changed (it will be different if it changes)
This object represents the local cache, which can be used to notify the user that the local cache has been updated, and also allows manual updating of the local cache.
When the browser updates the local cache and loads a new resource file, the updateready event of the applicationCache object will be triggered, notifying that the local cache has been modified, and then prompting the user to manually refresh the page.
swapCache
The swapCache method is used to manually perform local cache updates. It can only be called when the updateReady event of the applicationCache object is triggered,
That is, when the resource file changes, you can use this method to manually cache the update.