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 [configCookieSync()](../reference/apis-arkweb/js-apis-webview.md#configcookiesync11) as an example to describe how to set a cookie's value to **value=test** for **www\.example.com**. For details about functions and usage of other APIs, see [WebCookieManager()](../reference/apis-arkweb/js-apis-webview.md#webcookiemanager). 11 12 13```ts 14// xxx.ets 15import { webview } from '@kit.ArkWeb'; 16import { BusinessError } from '@kit.BasicServicesKit'; 17 18@Entry 19@Component 20struct WebComponent { 21 controller: webview.WebviewController = new webview.WebviewController(); 22 23 build() { 24 Column() { 25 Button('configCookieSync') 26 .onClick(() => { 27 try { 28 webview.WebCookieManager.configCookieSync('https://www.example.com', 'value=test'); 29 } catch (error) { 30 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 31 } 32 }) 33 Web({ src: 'www.example.com', controller: this.controller }) 34 } 35 } 36} 37``` 38 39 40## Cache and Storage Management 41 42Network resource requests are relatively time-consuming during website access. You can store resources locally by means of **cache** and **Dom Storage** to fasten the access to the same website. 43 44 45### Cache 46 47Use [cacheMode()](../reference/apis-arkweb/ts-basic-components-web.md#cachemode) to configure the cache mode for page resources. Four cache modes are supported: 48 49- **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. 50 51- **None**: Page resources are loaded from the cache. If the cache does not exist, page resources are obtained from the network. 52 53- **Online**: Page resources are not loaded from the cache. All resources are obtained from the network. 54 55- **Only**: Page resources are only loaded from the cache. 56 57 58In the following example, the cache mode is set to **None**. 59 60 61 62```ts 63// xxx.ets 64import { webview } from '@kit.ArkWeb'; 65 66@Entry 67@Component 68struct WebComponent { 69 @State mode: CacheMode = CacheMode.None; 70 controller: webview.WebviewController = new webview.WebviewController(); 71 72 build() { 73 Column() { 74 Web({ src: 'www.example.com', controller: this.controller }) 75 .cacheMode(this.mode) 76 } 77 } 78} 79``` 80 81 82 To obtain up-to-date resources, you can use [removeCache()](../reference/apis-arkweb/js-apis-webview.md#removecache) to clear cached resources. The sample code is as follows: 83 84```ts 85// xxx.ets 86import { webview } from '@kit.ArkWeb'; 87import { BusinessError } from '@kit.BasicServicesKit'; 88 89@Entry 90@Component 91struct WebComponent { 92 @State mode: CacheMode = CacheMode.None; 93 controller: webview.WebviewController = new webview.WebviewController(); 94 95 build() { 96 Column() { 97 Button('removeCache') 98 .onClick(() => { 99 try { 100 // 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. 101 this.controller.removeCache(true); 102 } catch (error) { 103 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 104 } 105 }) 106 Web({ src: 'www.example.com', controller: this.controller }) 107 .cacheMode(this.mode) 108 } 109 } 110} 111``` 112 113 114### Dom Storage 115 116Dom 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/apis-arkweb/ts-basic-components-web.md#domstorageaccess) to enable Dom Storage. The following is the sample code: 117 118 119 120```ts 121// xxx.ets 122import { webview } from '@kit.ArkWeb'; 123 124@Entry 125@Component 126struct WebComponent { 127 controller: webview.WebviewController = new webview.WebviewController(); 128 129 build() { 130 Column() { 131 Web({ src: 'www.example.com', controller: this.controller }) 132 .domStorageAccess(true) 133 } 134 } 135} 136``` 137