Principle:Puppeteer Puppeteer Request Interception
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Networking, Testing |
| Last Updated | 2026-02-11 23:00 GMT |
Overview
A mechanism that intercepts, inspects, modifies, or blocks HTTP requests made by a browser page before they reach the network.
Description
Request Interception allows automation scripts to control the network layer of a browser page. When enabled, every HTTP request the page makes is paused and exposed to the script, which can then:
- Continue the request unchanged (or with modifications to URL, headers, method, or body)
- Abort the request to prevent it from reaching the network
- Respond with a custom response, bypassing the network entirely
Common use cases:
- Blocking ads, trackers, or unnecessary resources (images, fonts) to speed up page loads
- Mocking API responses for testing
- Modifying request headers (authentication, custom headers)
- Logging and monitoring network activity
- Simulating network errors
The interception system supports cooperative handling: multiple handlers can be registered, and a priority-based resolution determines the final action.
Usage
Use request interception when you need to control, modify, or monitor the network requests a page makes. Enable interception before navigating to the target URL. Every intercepted request must be explicitly continued, aborted, or responded to; failing to handle a request will cause it to stall.
Theoretical Basis
# Request interception pipeline
1. page.setRequestInterception(true) enables interception
2. page.on('request', handler) registers handler
3. Page makes HTTP request (navigation, fetch, XHR, resource)
4. Request is paused and handler is called with HTTPRequest object
5. Handler inspects: request.url(), request.resourceType(), request.method()
6. Handler decides:
a. request.continue() → forward to network (optionally modified)
b. request.abort() → cancel with error code
c. request.respond() → return custom response
7. Browser processes the decision