• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Cookies and Data Storage
2
3
4## Cookie Management
5
6A cookie is a segment of data sent from the server to the client to uniquely identify a user during network access. The client may hold the data and provide it to the server at later interactions so that the server can quickly identify the client identity and status.
7
8The **Web** component provides the **WebCookieManager** class for you to manage cookie information, which is stored in the **/proc/{pid}/root/data/storage/el2/base/cache/web/Cookiesd** file in the application sandbox path.
9
10The following uses [setCookie()](../reference/apis/js-apis-webview.md#setcookie) as an example to describe how to set a cookie's value to **test** for **www.example.com**. For details about functions and usage of other APIs, see [WebCookieManager()](../reference/apis/js-apis-webview.md#webcookiemanager).
11
12
13```ts
14// xxx.ets
15import web_webview from '@ohos.web.webview';
16
17@Entry
18@Component
19struct WebComponent {
20  controller: web_webview.WebviewController = new web_webview.WebviewController();
21
22  build() {
23    Column() {
24      Button('setCookie')
25        .onClick(() => {
26          try {
27            web_webview.WebCookieManager.setCookie('https://www.example.com', 'value=test');
28          } catch (error) {
29            console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
30          }
31        })
32      Web({ src: 'www.example.com', controller: this.controller })
33    }
34  }
35}
36```
37
38
39## Cache and Storage Management
40
41Network resource requests are relatively time-consuming during website access. You can use store resources locally by means of cache and Dom Storage to fasten the access to the same website.
42
43
44### Cache
45
46Use [cacheMode()](../reference/arkui-ts/ts-basic-components-web.md#cachemode) to configure the cache mode for page resources. Four cache modes are supported:
47
48- **Default**: Page resources in a cache that has not expired are preferentially used. If the cache does not exist, page resources are obtained from the network.
49
50- **None**: Page resources are loaded from the cache. If the cache does not exist, page resources are obtained from the network.
51
52- **Online**: Page resources are not loaded from the cache. All resources are obtained from the network.
53
54- **Only**: Page resources are only loaded from the cache.
55
56
57In the following example, the cache mode is set to **None**.
58
59
60
61```ts
62// xxx.ets
63import web_webview from '@ohos.web.webview';
64
65@Entry
66@Component
67struct WebComponent {
68  @State mode: CacheMode = CacheMode.None;
69  controller: web_webview.WebviewController = new web_webview.WebviewController();
70  build() {
71    Column() {
72      Web({ src: 'www.example.com', controller: this.controller })
73        .cacheMode(this.mode)
74    }
75  }
76}
77```
78
79
80  To obtain up-to-date resources, you can use [removeCache()](../reference/apis/js-apis-webview.md#removecache) to clear cached resources. The sample code is as follows:
81
82```ts
83// xxx.ets
84import web_webview from '@ohos.web.webview';
85
86@Entry
87@Component
88struct WebComponent {
89  @State mode: CacheMode = CacheMode.None;
90  controller: web_webview.WebviewController = new web_webview.WebviewController();
91  build() {
92    Column() {
93      Button('removeCache')
94        .onClick(() => {
95          try {
96            // If this parameter is set to true, the cache in both the ROM and RAM is cleared. If this parameter is set to false, only the cache in the RAM is cleared.
97            this.controller.removeCache(true);
98          } catch (error) {
99            console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
100          }
101        })
102      Web({ src: 'www.example.com', controller: this.controller })
103        .cacheMode(this.mode)
104    }
105  }
106}
107```
108
109
110### Dom Storage
111
112Dom Storage falls into Session Storage and Local Storage. Wherein, Session Storage applies to the temporary data, and its data storage and release follow the session lifecycle; Local Storage applies to the persistent data, which is flushed to the application directory. In both storage modes, data is stored in a form of key-value pair, and is usually used when a page that needs to be stored on the client is accessed. You can use [domStorageAccess()](../reference/arkui-ts/ts-basic-components-web.md#domstorageaccess) to enable Dom Storage. The following is the sample code:
113
114
115
116```ts
117// xxx.ets
118import web_webview from '@ohos.web.webview';
119
120@Entry
121@Component
122struct WebComponent {
123  controller: web_webview.WebviewController = new web_webview.WebviewController();
124  build() {
125    Column() {
126      Web({ src: 'www.example.com', controller: this.controller })
127        .domStorageAccess(true)
128    }
129  }
130}
131```
132