1e41f4b71Sopenharmony_ci# Lifecycle of Web Components 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciYou can use **Web** components to load local or online web pages. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe **Web** components provide various component lifecycle callbacks. Using these callbacks, you can detect the lifecycle changes of **Web** components and process related services. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe states of a **Web** component include: controller binding to the **Web** component, web page loading start, web page loading progress, web page loading end, and the page that is about to be visible. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci**Figure 1** shows callback events during the normal loading of the web page of the **Web** component 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Web Page Loading States of the Web component 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- [aboutToAppear](../reference/apis-arkui/arkui-ts/ts-custom-component-lifecycle.md#abouttoappear): When a new instance of a custom component is created, execute this function before its build function is executed. In this case, you are advised to invoke APIs such as [setWebDebuggingAccess](../reference/apis-arkweb/js-apis-webview.md#setwebdebuggingaccess), [customizeSchemes](../reference/apis-arkweb/js-apis-webview.md#customizeschemes), and [configCookie](../reference/apis-arkweb/js-apis-webview.md#configcookie11-1). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- [onControllerAttached](../reference/apis-arkweb/ts-basic-components-web.md#oncontrollerattached10): This callback is triggered when the controller is successfully attached to the **Web** component. You are advised to inject the JavaScript object [registerJavaScriptProxy](../reference/apis-arkweb/js-apis-webview.md#registerjavascriptproxy) and set the custom user agent [setCustomUserAgent](../reference/apis-arkweb/js-apis-webview.md#setcustomuseragent10) in this event. In the callback, you can use APIs irrelevant to web page operations, such as [loadUrl](../reference/apis-arkweb/js-apis-webview.md#loadurl) and [getWebId](../reference/apis-arkweb/js-apis-webview.md#getwebid). However, the web page is not loaded when the callback function is called. Therefore, APIs for web page operation, such as [zoomIn](../reference/apis-arkweb/js-apis-webview.md#zoomin) and [zoomOut](../reference/apis-arkweb/js-apis-webview.md#zoomout), cannot be used in the callback function. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- [onLoadIntercept](../reference/apis-arkweb/ts-basic-components-web.md#onloadintercept10) : This callback is triggered before the **Web** component loads the URL, which is used to determine whether to block the access. By default, the loading is allowed. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci- [onOverrideUrlLoading](../reference/apis-arkweb/ts-basic-components-web.md#onoverrideurlloading12): This callback is triggered when the URL is to be loaded to the web. By using this callback, the host application can obtain the control right. If the callback returns **true**, the web stops loading the URL. If the callback returns **false**, the web continues to load the URL. The behavior of **onLoadIntercept()** is different from that of the **onOverrideUrlLoading()** and they are triggered in different timing. Therefore, the two APIs are used in different scenarios. When **LoadUrl** and **iframe** (HTML tag, indicating the HTML inline framework element, which is used to embed another page into the current page) are loaded, **onLoadIntercept()** is triggered, but **onOverrideUrlLoading()** is not triggered when **LoadUrl** is loaded and when the **iframe** loads the HTTP(s) protocol or **about:blank**. For details, see [onLoadIntercept](../reference/apis-arkweb/ts-basic-components-web.md#onloadintercept10) and [onOverrideUrlLoading](../reference/apis-arkweb/ts-basic-components-web.md#onoverrideurlloading12). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci- [onInterceptRequest](../reference/apis-arkweb/ts-basic-components-web.md#oninterceptrequest9): This callback is triggered before the **Web** component loads the URL, which is used to intercept the URL and return response data. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- [onPageBegin](../reference/apis-arkweb/ts-basic-components-web.md#onpagebegin): This callback is triggered when a web page starts to be loaded and is triggered only in the main frame (an HTML element used to display the HTML page). This callback is not triggered when the content of an **iframe** or **frameset** (an HTML tag used to include frames) is loaded. The multi-frame page may start to be loaded at the same time. Even if the main frame is already loaded, the sub-frames may start to be loaded or continue to be loaded. This callback is not triggered for navigation (such as segment and historical status) on the same page, navigation that fails before submission, or navigation that is canceled. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci- [onProgressChange](../reference/apis-arkweb/ts-basic-components-web.md#onprogresschange): This callback is triggered to notify the loading progress of the page. The multi-frame page or sub-frames may continue to be loaded while the main frame is already loaded. Therefore, this callback may still be received after [onPageEnd](../reference/apis-arkweb/ts-basic-components-web.md#onpageend) is triggered. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- [onPageEnd](../reference/apis-arkweb/ts-basic-components-web.md#onpageend): This callback is triggered only in the main frame when the web page is loaded. The multi-frame page may start to be loaded at the same time. Even if the main frame is already loaded, the sub-frame may start to be loaded or continue to be loaded. This callback is not triggered for navigation (such as segment and historical status) on the same page, navigation that fails before submission, or navigation that is canceled. You are advised to execute the JavaScript script [loadUrl](../reference/apis-arkweb/js-apis-webview.md#runjavascript) in this callback. Note that receiving this callback does not guarantee that the next frame of drawn by the **Web** will reflect the current DOM status. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- [onPageVisible](../reference/apis-arkweb/ts-basic-components-web.md#onpagevisible9): This callback is triggered when the body of an HTTP response starts to be loaded and a new page is about to be visible. This callback is triggered only in the main frame. This callback is triggered early in document loading, so linked resources such as online CSS and online images may not be available. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci- [onRenderExited](../reference/apis-arkweb/ts-basic-components-web.md#onrenderexited9): This callback function is triggered when the application rendering process exits abnormally. You can release system resources and save data in this callback. If you want to recover the application from an exception, call the [loadUrl](../reference/apis-arkweb/js-apis-webview.md#loadurl) API to reload the page. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci- [onDisAppear](../reference/apis-arkui/arkui-ts/ts-universal-events-show-hide.md#ondisappear) : This callback is triggered when the component is uninstalled. This event is a common event triggered when a component is uninstalled from the component tree. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciCodes on the application side: 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci ```ts 42e41f4b71Sopenharmony_ci // xxx.ets 43e41f4b71Sopenharmony_ci import { webview } from '@kit.ArkWeb'; 44e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 45e41f4b71Sopenharmony_ci import { promptAction } from '@kit.ArkUI'; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci @Entry 48e41f4b71Sopenharmony_ci @Component 49e41f4b71Sopenharmony_ci struct WebComponent { 50e41f4b71Sopenharmony_ci controller: webview.WebviewController = new webview.WebviewController(); 51e41f4b71Sopenharmony_ci responseWeb: WebResourceResponse = new WebResourceResponse(); 52e41f4b71Sopenharmony_ci heads: Header[] = new Array(); 53e41f4b71Sopenharmony_ci @State webData: string = "<!DOCTYPE html>\n" + 54e41f4b71Sopenharmony_ci "<html>\n" + 55e41f4b71Sopenharmony_ci "<head>\n" + 56e41f4b71Sopenharmony_ci "<title>intercept test</title>\n" + 57e41f4b71Sopenharmony_ci "</head>\n" + 58e41f4b71Sopenharmony_ci "<body>\n" + 59e41f4b71Sopenharmony_ci "<h1>intercept test</h1>\n" + 60e41f4b71Sopenharmony_ci "</body>\n" + 61e41f4b71Sopenharmony_ci "</html>"; 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci aboutToAppear(): void { 64e41f4b71Sopenharmony_ci try { 65e41f4b71Sopenharmony_ci webview.WebviewController.setWebDebuggingAccess(true); 66e41f4b71Sopenharmony_ci } catch (error) { 67e41f4b71Sopenharmony_ci console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci build() { 72e41f4b71Sopenharmony_ci Column() { 73e41f4b71Sopenharmony_ci Web({ src: $rawfile('index.html'), controller: this.controller }) 74e41f4b71Sopenharmony_ci .onControllerAttached(() => { 75e41f4b71Sopenharmony_ci // You are advised to use **loadUrl**, set a custom user agent, and inject a JS object. 76e41f4b71Sopenharmony_ci console.log('onControllerAttached execute') 77e41f4b71Sopenharmony_ci }) 78e41f4b71Sopenharmony_ci .onLoadIntercept((event) => { 79e41f4b71Sopenharmony_ci if (event) { 80e41f4b71Sopenharmony_ci console.log('onLoadIntercept url:' + event.data.getRequestUrl()) 81e41f4b71Sopenharmony_ci console.log('url:' + event.data.getRequestUrl()) 82e41f4b71Sopenharmony_ci console.log('isMainFrame:' + event.data.isMainFrame()) 83e41f4b71Sopenharmony_ci console.log('isRedirect:' + event.data.isRedirect()) 84e41f4b71Sopenharmony_ci console.log('isRequestGesture:' + event.data.isRequestGesture()) 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci // If true is returned, the loading is blocked. Otherwise, the loading is allowed. 87e41f4b71Sopenharmony_ci return true 88e41f4b71Sopenharmony_ci }) 89e41f4b71Sopenharmony_ci .onOverrideUrlLoading((webResourceRequest: WebResourceRequest) => { 90e41f4b71Sopenharmony_ci if (webResourceRequest && webResourceRequest.getRequestUrl() == "about:blank") { 91e41f4b71Sopenharmony_ci return true; 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci return false; 94e41f4b71Sopenharmony_ci }) 95e41f4b71Sopenharmony_ci .onInterceptRequest((event) => { 96e41f4b71Sopenharmony_ci if (event) { 97e41f4b71Sopenharmony_ci console.log('url:' + event.request.getRequestUrl()); 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci let head1: Header = { 100e41f4b71Sopenharmony_ci headerKey: "Connection", 101e41f4b71Sopenharmony_ci headerValue: "keep-alive" 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci let head2: Header = { 104e41f4b71Sopenharmony_ci headerKey: "Cache-Control", 105e41f4b71Sopenharmony_ci headerValue: "no-cache" 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci let length = this.heads.push(head1); 108e41f4b71Sopenharmony_ci length = this.heads.push(head2); 109e41f4b71Sopenharmony_ci this.responseWeb.setResponseHeader(this.heads); 110e41f4b71Sopenharmony_ci this.responseWeb.setResponseData(this.webData); 111e41f4b71Sopenharmony_ci this.responseWeb.setResponseEncoding('utf-8'); 112e41f4b71Sopenharmony_ci this.responseWeb.setResponseMimeType('text/html'); 113e41f4b71Sopenharmony_ci this.responseWeb.setResponseCode(200); 114e41f4b71Sopenharmony_ci this.responseWeb.setReasonMessage('OK'); 115e41f4b71Sopenharmony_ci // If response data is returned, the data is loaded based on the response data. If no response data is returned, null is returned, indicating that the data is loaded in the original mode. 116e41f4b71Sopenharmony_ci return this.responseWeb; 117e41f4b71Sopenharmony_ci }) 118e41f4b71Sopenharmony_ci .onPageBegin((event) => { 119e41f4b71Sopenharmony_ci if (event) { 120e41f4b71Sopenharmony_ci console.log('onPageBegin url:' + event.url); 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci }) 123e41f4b71Sopenharmony_ci .onFirstContentfulPaint(event => { 124e41f4b71Sopenharmony_ci if (event) { 125e41f4b71Sopenharmony_ci console.log("onFirstContentfulPaint:" + "[navigationStartTick]:" + 126e41f4b71Sopenharmony_ci event.navigationStartTick + ", [firstContentfulPaintMs]:" + 127e41f4b71Sopenharmony_ci event.firstContentfulPaintMs); 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci }) 130e41f4b71Sopenharmony_ci .onProgressChange((event) => { 131e41f4b71Sopenharmony_ci if (event) { 132e41f4b71Sopenharmony_ci console.log('newProgress:' + event.newProgress); 133e41f4b71Sopenharmony_ci } 134e41f4b71Sopenharmony_ci }) 135e41f4b71Sopenharmony_ci .onPageEnd((event) => { 136e41f4b71Sopenharmony_ci // You are advised to execute the JavaScript script in this event. 137e41f4b71Sopenharmony_ci if (event) { 138e41f4b71Sopenharmony_ci console.log('onPageEnd url:' + event.url); 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci }) 141e41f4b71Sopenharmony_ci .onPageVisible((event) => { 142e41f4b71Sopenharmony_ci console.log('onPageVisible url:' + event.url); 143e41f4b71Sopenharmony_ci }) 144e41f4b71Sopenharmony_ci .onRenderExited((event) => { 145e41f4b71Sopenharmony_ci if (event) { 146e41f4b71Sopenharmony_ci console.log('onRenderExited reason:' + event.renderExitReason); 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci }) 149e41f4b71Sopenharmony_ci .onDisAppear(() => { 150e41f4b71Sopenharmony_ci promptAction.showToast({ 151e41f4b71Sopenharmony_ci message: 'The web is hidden', 152e41f4b71Sopenharmony_ci duration: 2000 153e41f4b71Sopenharmony_ci }) 154e41f4b71Sopenharmony_ci }) 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci } 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci ``` 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ciFrontend index.html. 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci ```html 163e41f4b71Sopenharmony_ci <!-- index.html --> 164e41f4b71Sopenharmony_ci <!DOCTYPE html> 165e41f4b71Sopenharmony_ci <html> 166e41f4b71Sopenharmony_ci <head> 167e41f4b71Sopenharmony_ci <meta charset="UTF-8"> 168e41f4b71Sopenharmony_ci </head> 169e41f4b71Sopenharmony_ci <body> 170e41f4b71Sopenharmony_ci <h1>Hello, ArkWeb</h1> 171e41f4b71Sopenharmony_ci </body> 172e41f4b71Sopenharmony_ci </html> 173e41f4b71Sopenharmony_ci ``` 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci## Performance Indicators Loaded by the Web Page of Web Components 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ciPay attention to some important performance indicators during web page loading. Such as First Contentful Paint (FCP), First Meaningful Paint (FMP), and Last Contentful Paint (LCP). The **Web** component provides the following APIs: 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci- [onFirstContentfulPaint](../reference/apis-arkweb/ts-basic-components-web.md#onfirstcontentfulpaint10): This callback is triggered when the web page content is first rendered. Time point when a text, image, non-blank Canvva, or SVG is drawn for the first time. 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci- [onFirstMeaningfulPaint](../reference/apis-arkweb/ts-basic-components-web.md#onfirstmeaningfulpaint12): This callback is triggered when a meaningful web page is first rendered. Time when the main content of the page is rendered for the first time. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci- [onLargestContentfulPaint](../reference/apis-arkweb/ts-basic-components-web.md#onlargestcontentfulpaint12) : This callback is triggered when the largest contentful web page is rendered. Time when the visible element with the largest content in the visible area starts to appear on the page. 184