1. Background Nowadays, App development uses Hybrid mode to some extent. When it comes to WebView, some js files are often loaded (such as bridge.js for Native communication with WebView). These js files do not change frequently, so we hope that after loading js in WebView once, if js does not change, there is no need to initiate a network request to load it next time, thereby reducing traffic and resource usage. So what are the ways to achieve this goal? First, we have to start with the cache principle of WebView. 2. WebView cache type WebView mainly includes two types of caches. One is the browser's built-in web page data cache, which is supported by all browsers and defined by the HTTP protocol. The other is the H5 cache, which is set by the web page developer. The H5 cache mainly includes App Cache, DOM Storage, Local Storage, Web SQL Database storage mechanisms, etc. Here we mainly introduce App Cache to cache js files. 3. Browser's built-in web data cache 1. Working Principle The browser cache mechanism controls file cache through fields such as Cache-Control (or Expires) and Last-Modified (or Etag) in the HTTP protocol header. For the functions of these fields and the browser cache update mechanism, you can read these two articles (H5 Cache Mechanism Analysis of Mobile Web Loading Performance Optimization, Android: Hand-in-hand Teach You to Build WebView's Cache Mechanism & Resource Preloading Solution), which have detailed introductions. Below, from the perspective of my actual application, I will introduce the headers that are usually encountered in the HTTP protocol. These two fields are used by the browser to decide whether the file needs to be cached when receiving a response, or whether a request needs to be made when a file needs to be loaded.
The following two fields are used by the server to determine whether the file needs to be updated when a request is initiated.
2. How to set WebView to support the above protocols From the above introduction, we can know that any mainstream and qualified browser should be able to support these fields at the HTTP protocol level. This is not something that we developers can modify, nor is it a configuration that we should modify. On Android, our WebView also supports these fields. However, we can set the Cache Mode of WebView through code to make the protocol effective or invalid. WebView has the following Cache Modes:
The sample code for setting the Cache Mode of WebView cache is as follows:
Many people on the Internet say that you should choose the Cache Mode according to the network conditions. When there is a network, set it to LOAD_DEFAULT, and when there is no network, set it to LOAD_CACHE_ELSE_NETWORK. But in my business, the updates of js files are all non-overwriting updates, that is, every time you change the js file, the URL address of the file will definitely change, so I hope that the browser can cache the js and keep using it, so I will only set it to LOAD_CACHE_ELSE_NETWORK. Of course, if you can change the Cache-Control field of the cdn server of js, that's fine, just use LOAD_DEFAULT. As for whether the file should be updated in an overwriting or non-overwriting manner, it is not what I want to discuss today. In the field of web front-end, this is a topic that can be discussed. Regarding iOS WebView, my colleague discovered during actual testing that the Response Header that controls file caching is the Expires field. And iOS cannot set the Cache Mode for the entire WebView, it can only be set for each URLRequest. I will have the opportunity to learn more about iOS later. 3. Storage path in the phone How are the files cached by the browser stored? This question has been a mystery since I came into contact with WebView. This time, due to work needs, I specially rooted two mobile phones, a Redmi 1 (Android 4.4) and a Xiaomi 4c (Android 5.1). After rooting two Nexus phones with high system versions (6.0 and 7.1) failed, I decided to take a look at where the cache of WebView is stored on 4.4 and 5.1 systems. First of all, you don’t have to think about it, you know that these files must be in the /data/data/package name/directory. As mentioned in one of my previous blogs, this is the internal storage directory of each application. Next, we open the terminal, use adb to connect to the phone, and then follow the commands below.
You may have noticed that the first folder is called Application Cache, we will talk about it later.
However, on the 5.1 system, the /data/data/package name/app_webview/ folder still exists, but the app_webview/cache folder that stores the WebView's built-in cache on the 4.4 system no longer exists (note that the App Cache directory is still there), as shown in the figure below. In summary, the cache supported by the browser protocol of WebView is located in different locations on different system versions. Perhaps except for 4.4 and 5.1 that I have rooted, the cache of WebView in other versions of the system may also exist in different directories. Another thing is the storage format and index format of cache files, which may be different on different phones. I have seen people online saying that there is a file called webview.db or webviewCache.db. This file is not under app_webview/cache or org.chromium.android_webview, but in /data/data/package name/database/. However, I did not see this file on my two rooted phones, and I opened all the db files under /data/data/package name/ and did not find any table storing URL records. In fact, taking the 5.1 system as an example, I saw files called index and /index-dir/the-real-index under /data/data/package-name/cache/org.chromium.android_webview/, as well as a bunch of files named md5+underscore+number, which can also be seen in the picture above. I still have some questions about the principle of this, and I hope that professional experts can answer them. 4. H5 Cache After talking about the built-in cache of WebView, let's talk about the App Cache in H5. This cache is controlled by the developer of the web page, not by Native, but the WebView in Native also needs us to make some settings to support this feature of H5. 1. Working Principle When writing web page code, specify the manifest attribute to enable the page to use App Cache. Usually the HTML page code is written like this:
The xxx.appcache file uses a relative path, in which case the path of the appcache file is the same as the page. You can also use an absolute path, but the domain name must be consistent with the page. A complete xxx.appcache file generally includes three sections, and the basic format is as follows:
How AppCache works: When an HTML page with a manifest file is loaded, the file specified by CACHE MANIFEST will be cached in the browser's App Cache directory. When the page is loaded next time, the file cached by the manifest will be applied first, and then a request to load the xxx.appcache file will be sent to the server. If the xxx.appcache file has not been modified, the server will return 304 Not Modified to the browser. If the xxx.appcache file has been modified, the server will return 200 OK and return the content of the new xxx.appcache file to the browser. After receiving it, the browser will load the content specified in the new xxx.appcache file for caching. As you can see, AppCache needs to send a xxx.appcache request every time a page is loaded to check if the manifest file has been updated (byte by byte). AppCache has some pitfalls and is no longer officially recommended, but it is still supported by mainstream browsers. The article mainly mentions the following pitfalls:
2. How to set WebView to support AppCache AppCache support is not enabled by default in WebView. You need to add the following lines of code to set it up:
Note: Both setAppCacheEnabled and setAppCachePath of WebSettings must be called. 3. Path to store AppCache According to the API description of Android SDK, setAppCachePath can be used to set the AppCache path, but I actually tested and found that no matter how you set this path, whether it is set to the application's own internal private directory or the external SD card, it will not take effect. AppCache cache files will eventually be stored in the folder /data/data/package name/app_webview/cache/Application Cache, which can be seen in the screenshots of the Android 4.4 and 5.1 system directories above, but if you do not call the setAppCachePath method, WebView will not generate this directory. This is a bit strange to me. I guess that starting from a certain system version, in order to consider the integrity and security of the cached files, the SDK set the AppCache cache directory to the internal private storage when implementing it. V. Conclusion Similarities The built-in cache of WebView and AppCache can both be used for file-level caching, which basically meets the needs of non-overlapping js, css and other file updates. Differences
***In fact, in many cases, these two types of caches work together. When the manifest file does not control the loading of certain resources, for example, in the xxx.appcache file I wrote above, the * sign is used under the NETWORK section, which means that all non-cached files must be loaded from the network. At this time, these resources will go to the WebView's built-in cache mechanism. Combined with WebView's CacheMode, we actually cache these files in WebView's built-in cache. Understanding the principles of these two types of caches will help us better design our own pages and apps, reduce network requests as much as possible, and improve App operation efficiency. |
<<: Use Node.js to segment text content and extract keywords
[[383475]] Recently, Google has released the firs...
"Where there is a question, there is an answ...
As we all know, Apple has gone against its norm t...
Neutrinos may be the most fascinating elementary ...
This is not the first time I’ve shared this exper...
Cold, cold, cold... Have you felt the sudden drop...
Ma Fang's 12 Lectures on "12 Principles ...
In recent years, with the gradual development of ...
This article takes the education industry as an e...
Customer acquisition has always been an eternal t...
Introduction: When uploading new products to the ...
Produced by: Science Popularization China Author:...
If the benefits of your product are not clear, th...
Apophobia is the fear of falling, which most huma...