1e41f4b71Sopenharmony_ci# Managing Cookies and Data Storage 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Cookie Management 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciA 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. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ciThe **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. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ciThe 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). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ci// xxx.ets 15e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 16e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci@Entry 19e41f4b71Sopenharmony_ci@Component 20e41f4b71Sopenharmony_cistruct WebComponent { 21e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci build() { 24e41f4b71Sopenharmony_ci Column() { 25e41f4b71Sopenharmony_ci Button('configCookieSync') 26e41f4b71Sopenharmony_ci .onClick(() => { 27e41f4b71Sopenharmony_ci try { 28e41f4b71Sopenharmony_ci webview.WebCookieManager.configCookieSync('https://www.example.com', 'value=test'); 29e41f4b71Sopenharmony_ci } catch (error) { 30e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 31e41f4b71Sopenharmony_ci } 32e41f4b71Sopenharmony_ci }) 33e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller }) 34e41f4b71Sopenharmony_ci } 35e41f4b71Sopenharmony_ci } 36e41f4b71Sopenharmony_ci} 37e41f4b71Sopenharmony_ci``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## Cache and Storage Management 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciNetwork 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. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci### Cache 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciUse [cacheMode()](../reference/apis-arkweb/ts-basic-components-web.md#cachemode) to configure the cache mode for page resources. Four cache modes are supported: 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci- **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. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci- **None**: Page resources are loaded from the cache. If the cache does not exist, page resources are obtained from the network. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci- **Online**: Page resources are not loaded from the cache. All resources are obtained from the network. 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci- **Only**: Page resources are only loaded from the cache. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciIn the following example, the cache mode is set to **None**. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci```ts 63e41f4b71Sopenharmony_ci// xxx.ets 64e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci@Entry 67e41f4b71Sopenharmony_ci@Component 68e41f4b71Sopenharmony_cistruct WebComponent { 69e41f4b71Sopenharmony_ci @State mode: CacheMode = CacheMode.None; 70e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci build() { 73e41f4b71Sopenharmony_ci Column() { 74e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller }) 75e41f4b71Sopenharmony_ci .cacheMode(this.mode) 76e41f4b71Sopenharmony_ci } 77e41f4b71Sopenharmony_ci } 78e41f4b71Sopenharmony_ci} 79e41f4b71Sopenharmony_ci``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci 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: 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci```ts 85e41f4b71Sopenharmony_ci// xxx.ets 86e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 87e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci@Entry 90e41f4b71Sopenharmony_ci@Component 91e41f4b71Sopenharmony_cistruct WebComponent { 92e41f4b71Sopenharmony_ci @State mode: CacheMode = CacheMode.None; 93e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci build() { 96e41f4b71Sopenharmony_ci Column() { 97e41f4b71Sopenharmony_ci Button('removeCache') 98e41f4b71Sopenharmony_ci .onClick(() => { 99e41f4b71Sopenharmony_ci try { 100e41f4b71Sopenharmony_ci // 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. 101e41f4b71Sopenharmony_ci this.controller.removeCache(true); 102e41f4b71Sopenharmony_ci } catch (error) { 103e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci }) 106e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller }) 107e41f4b71Sopenharmony_ci .cacheMode(this.mode) 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci} 111e41f4b71Sopenharmony_ci``` 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci### Dom Storage 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ciDom 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: 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci```ts 121e41f4b71Sopenharmony_ci// xxx.ets 122e41f4b71Sopenharmony_ciimport { webview } from '@kit.ArkWeb'; 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci@Entry 125e41f4b71Sopenharmony_ci@Component 126e41f4b71Sopenharmony_cistruct WebComponent { 127e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci build() { 130e41f4b71Sopenharmony_ci Column() { 131e41f4b71Sopenharmony_ci Web({ src: 'www.example.com', controller: this.controller }) 132e41f4b71Sopenharmony_ci .domStorageAccess(true) 133e41f4b71Sopenharmony_ci } 134e41f4b71Sopenharmony_ci } 135e41f4b71Sopenharmony_ci} 136e41f4b71Sopenharmony_ci``` 137