1# Customizing Page Request Responses
2
3
4The **Web** component supports customization of the response to intercepted page requests. You can call [onInterceptRequest()](../reference/apis-arkweb/ts-basic-components-web.md#oninterceptrequest9) to customize web page responses, file resource responses, and more.
5
6
7When a resource loading request is initiated on a web page, the application layer will receive the request. The application layer then constructs a local resource response and sends it to the web kernel. On receiving the response, the web kernel parses the response and loads page resources accordingly.
8
9
10In the following example, the **Web** component intercepts the web page request **https://www.example.com/test.html** and constructs a custom response in the application code.
11
12
13- Code of the **index.html** page:
14
15  ```html
16  <!DOCTYPE html>
17  <html>
18  <head>
19      <meta charset="utf-8">
20  </head>
21  <body>
22  <!-- Page resource request ->
23  <a href="https://www.example.com/test.html">intercept test!</a>
24  </body>
25  </html>
26  ```
27
28- Application code:
29
30  ```ts
31  // xxx.ets
32  import { webview } from '@kit.ArkWeb';
33
34  @Entry
35  @Component
36  struct WebComponent {
37    controller: webview.WebviewController = new webview.WebviewController();
38    responseResource: WebResourceResponse = new WebResourceResponse();
39    // Customize a response.
40    @State webData: string = '<!DOCTYPE html>\n' +
41      '<html>\n' +
42      '<head>\n' +
43      '<title>intercept test</title>\n' +
44      '</head>\n' +
45      '<body>\n' +
46      '<h1>intercept ok</h1>\n' +
47      '</body>\n' +
48      '</html>'
49
50    build() {
51      Column() {
52        Web({ src: $rawfile('index.html'), controller: this.controller })
53          .onInterceptRequest((event) => {
54            if (event) {
55              console.info('url:' + event.request.getRequestUrl());
56              // Intercept the web page request.
57              if (event.request.getRequestUrl() !== 'https://www.example.com/test.html') {
58                return null;
59              }
60            }
61            // Construct a custom response.
62            this.responseResource.setResponseData(this.webData);
63            this.responseResource.setResponseEncoding('utf-8');
64            this.responseResource.setResponseMimeType('text/html');
65            this.responseResource.setResponseCode(200);
66            this.responseResource.setReasonMessage('OK');
67            return this.responseResource;
68          })
69      }
70    }
71  }
72  ```
73
74Create a **CodeCache** object for a custom JS request response: If the resource of a custom request response is a JavaScript script, you can add the **ResponseDataID** field to the response header. After obtaining this field, the **Web** kernel generates a **CodeCache** object, which accelerates JavaScript execution. Any changes to the **ResponseDataID** field must be notified to the **Web** component. If the **ResponseDataID** field is not added, no **CodeCache** object is created by default.
75
76In the following example, the **Web** component intercepts the web page request **https://www.example.com/test.js**; a custom response is constructed in the application code, with the **ResponseDataID** field added to the response header.
77
78- Code of the **index.html** page:
79
80  ```html
81  <!DOCTYPE html>
82  <html>
83  <head>
84      <meta charset="utf-8">
85  </head>
86  <body>
87
88  <div id="div-1">this is a test div</div>
89  <div id="div-2">this is a test div</div>
90  <div id="div-3">this is a test div</div>
91  <div id="div-4">this is a test div</div>
92  <div id="div-5">this is a test div</div>
93  <div id="div-6">this is a test div</div>
94  <div id="div-7">this is a test div</div>
95  <div id="div-8">this is a test div</div>
96  <div id="div-9">this is a test div</div>
97  <div id="div-10">this is a test div</div>
98  <div id="div-11">this is a test div</div>
99
100  <script src="https://www.example.com/test.js"></script>
101  </body>
102  </html>
103  ```
104
105- Application code:
106
107  ```ts
108  // xxx.ets
109  import { webview } from '@kit.ArkWeb';
110
111  @Entry
112  @Component
113  struct WebComponent {
114    controller: webview.WebviewController = new webview.WebviewController();
115    responseResource: WebResourceResponse = new WebResourceResponse();
116    // Customize response data. (The CodeCache object is created only when the response data length is greater than or equal to 1024 bytes.)
117    @State jsData: string = 'let text_msg = "the modified content:version 0000000000001";\n' +
118      'let element1 = window.document.getElementById("div-1");\n' +
119      'let element2 = window.document.getElementById("div-2");\n' +
120      'let element3 = window.document.getElementById("div-3");\n' +
121      'let element4 = window.document.getElementById("div-4");\n' +
122      'let element5 = window.document.getElementById("div-5");\n' +
123      'let element6 = window.document.getElementById("div-6");\n' +
124      'let element7 = window.document.getElementById("div-7");\n' +
125      'let element8 = window.document.getElementById("div-8");\n' +
126      'let element9 = window.document.getElementById("div-9");\n' +
127      'let element10 = window.document.getElementById("div-10");\n' +
128      'let element11 = window.document.getElementById("div-11");\n' +
129      'element1.innerHTML = text_msg;\n' +
130      'element2.innerHTML = text_msg;\n' +
131      'element3.innerHTML = text_msg;\n' +
132      'element4.innerHTML = text_msg;\n' +
133      'element5.innerHTML = text_msg;\n' +
134      'element6.innerHTML = text_msg;\n' +
135      'element7.innerHTML = text_msg;\n' +
136      'element8.innerHTML = text_msg;\n' +
137      'element9.innerHTML = text_msg;\n' +
138      'element10.innerHTML = text_msg;\n' +
139      'element11.innerHTML = text_msg;\n';
140
141    build() {
142      Column() {
143        Web({ src: $rawfile('index.html'), controller: this.controller })
144          .onInterceptRequest((event) => {
145            // Intercept the web page request.
146            if (event?.request.getRequestUrl() == 'https://www.example.com/test.js') {
147              // Construct a custom response.
148              this.responseResource.setResponseHeader([
149                {
150                  // The value is a string of a maximum of 13 digits. It is a JavaScript identifier and must be updated to maintain consistency with JavaScript.
151                  headerKey: "ResponseDataID",
152                  headerValue: "0000000000001"
153                }]);
154              this.responseResource.setResponseData(this.jsData);
155              this.responseResource.setResponseEncoding('utf-8');
156              this.responseResource.setResponseMimeType('application/javascript');
157              this.responseResource.setResponseCode(200);
158              this.responseResource.setReasonMessage('OK');
159              return this.responseResource;
160            }
161            return null;
162          })
163      }
164    }
165  }
166  ```
167