<titledata-rh="true">Request Interception | Puppeteer</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://pptr.dev/guides/network-interception"><metadata-rh="true"property="og:locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docsearch:counter"content="2"><metadata-rh="true"property="og:title"content="Request Interception | Puppeteer"><metadata-rh="true"name="description"content="Once request interception is enabled, every request will stall unless it's"><metadata-rh="true"property="og:description"content="Once request interception is enabled, every request will stall unless it's"><linkdata-rh="true"rel="icon"href="/img/favicon.ico"><linkdata-rh="true"rel="canonical"href="https://pptr.dev/guides/network-interception"><linkdata-rh="true"rel="alternate"href="https://pptr.dev/guides/network-interception"hreflang="en"><linkdata-rh="true"rel="alternate"href="https://pptr.dev/guides/network-interception"hreflang="x-default"><linkdata-rh="true"rel="preconnect"href="https://DVKY664LG7-dsn.algolia.net"crossorigin="anonymous"><linkrel="search"type="application/opensearchdescription+xml"title="Puppeteer"href="/opensearch.xml">
<h2class="anchor anchorWithStickyNavbar_LWe7"id="multiple-intercept-handlers-and-asynchronous-resolutions">Multiple Intercept Handlers and Asynchronous Resolutions<ahref="#multiple-intercept-handlers-and-asynchronous-resolutions"class="hash-link"aria-label="Direct link to Multiple Intercept Handlers and Asynchronous Resolutions"title="Direct link to Multiple Intercept Handlers and Asynchronous Resolutions"></a></h2>
<p>By default Puppeteer will raise a <code>Request is already handled!</code> exception if
<p>This example demonstrates two synchronous handlers working together:</p>
<divclass="language-ts codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-ts codeBlock_bY9V thin-scrollbar"style="color:#393A34;background-color:#f6f8fa"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">/*</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">This first handler will succeed in calling request.continue because the request interception has never been resolved.</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">*/</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain">page</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">on</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token string"style="color:#e3116c">'request'</span><spanclass="token punctuation"style="color:#393A34">,</span><spanclass="token plain"> interceptedRequest </span><spanclass="token operator"style="color:#393A34">=></span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">if</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">interceptedRequest</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">isInterceptResolutionHandled</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">return</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"> interceptedRequest</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">continue</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">}</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">/*</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">This second handler will return before calling request.abort because request.continue was already</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">called by the first handler.</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">*/</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain">page</span><spanclass="token punctuation"s
<p>This example demonstrates asynchronous handlers working together:</p>
<divclass="language-ts codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-ts codeBlock_bY9V thin-scrollbar"style="color:#393A34;background-color:#f6f8fa"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">/*</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">This first handler will succeed in calling request.continue because the request interception has never been resolved.</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">*/</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain">page</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">on</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token string"style="color:#e3116c">'request'</span><spanclass="token punctuation"style="color:#393A34">,</span><spanclass="token plain"> interceptedRequest </span><spanclass="token operator"style="color:#393A34">=></span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// The interception has not been handled yet. Control will pass through this guard.</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">if</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">interceptedRequest</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">isInterceptResolutionHandled</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">return</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// It is not strictly necessary to return a promise, but doing so will allow Puppeteer to await this handler.</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">return</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">new</span><spanclass="token plain"></span><spanclass="token class-name builtin">Promise</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">resolve </span><spanclass="token operator"style="color:#393A34">=></span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// Continue after 500ms</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token function"style="color:#d73a49">setTimeout</span><spancl
<p>For finer-grained introspection (see Cooperative Intercept Mode below), you may
synchronously before using <code>abort/continue/respond</code>.</p>
<p>Here is the example above rewritten using <code>request.interceptResolutionState</code></p>
<divclass="language-ts codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-ts codeBlock_bY9V thin-scrollbar"style="color:#393A34;background-color:#f6f8fa"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">/*</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">This first handler will succeed in calling request.continue because the request interception has never been resolved.</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">*/</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain">page</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">on</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token string"style="color:#e3116c">'request'</span><spanclass="token punctuation"style="color:#393A34">,</span><spanclass="token plain"> interceptedRequest </span><spanclass="token operator"style="color:#393A34">=></span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// The interception has not been handled yet. Control will pass through this guard.</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">const</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain">action</span><spanclass="token punctuation"style="color:#393A34">}</span><spanclass="token plain"></span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"> interceptedRequest</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">interceptResolutionState</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">if</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">action </span><spanclass="token operator"style="color:#393A34">===</span><spanclass="token plain"> InterceptResolutionAction</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token plain">AlreadyHandled</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">return</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// It is not strictly necessary to return a promise, but doing so will allow Puppeteer to await this handler.</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">return</span><spanclass="token plain"></span><spanclass="token keyword"style
<h2class="anchor anchorWithStickyNavbar_LWe7"id="cooperative-intercept-mode">Cooperative Intercept Mode<ahref="#cooperative-intercept-mode"class="hash-link"aria-label="Direct link to Cooperative Intercept Mode"title="Direct link to Cooperative Intercept Mode"></a></h2>
<p><code>request.abort</code>, <code>request.continue</code>, and <code>request.respond</code> can accept an
<h2class="anchor anchorWithStickyNavbar_LWe7"id="cooperative-request-continuation">Cooperative Request Continuation<ahref="#cooperative-request-continuation"class="hash-link"aria-label="Direct link to Cooperative Request Continuation"title="Direct link to Cooperative Request Continuation"></a></h2>
<p>Puppeteer requires <code>request.continue()</code> to be called explicitly or the request
<h2class="anchor anchorWithStickyNavbar_LWe7"id="upgrading-to-cooperative-intercept-mode-for-package-maintainers">Upgrading to Cooperative Intercept Mode for package maintainers<ahref="#upgrading-to-cooperative-intercept-mode-for-package-maintainers"class="hash-link"aria-label="Direct link to Upgrading to Cooperative Intercept Mode for package maintainers"title="Direct link to Upgrading to Cooperative Intercept Mode for package maintainers"></a></h2>
<p>If you are package maintainer and your package uses intercept handlers, you can
<code>setInterceptResolutionConfig(priority)</code> that works for their use case:</p>
<divclass="language-ts codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-ts codeBlock_bY9V thin-scrollbar"style="color:#393A34;background-color:#f6f8fa"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#393A34"><spanclass="token comment"style="color:#999988;font-style:italic">// Defaults to undefined which preserves Legacy Mode behavior</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">let</span><spanclass="token plain"> _priority </span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">undefined</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// Export a module configuration function</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">export</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">const</span><spanclass="token plain"></span><spanclass="token function-variable function"style="color:#d73a49">setInterceptResolutionConfig</span><spanclass="token plain"></span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">priority </span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"></span><spanclass="token number"style="color:#36acaa">0</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token plain"></span><spanclass="token operator"style="color:#393A34">=></span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token plain">_priority </span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"> priority</span><spanclass="token punctuation"style="color:#393A34">)</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token doc-comment comment"style="color:#999988;font-style:italic">/**</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token doc-comment comment"style="color:#999988;font-style:italic"> * Note that this handler uses `DEFAULT_INTERCEPT_RESOLUTION_PRIORITY` to "pass" on this request. It is important to use</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token doc-comment comment"style="color:#999988;font-style:italic"> * the default priority when your handler has no opinion on the request and the intent is to continue() by default.</span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token doc-comment comment"style="color:#999988;font-style:italic"> */</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain">page</span><spanclass="token punctuation"style="color:#393A34">.</span><spanclass="token function"style="color:#d73a49">on</span><spanclass="token punctuation"style="color:#393A34">(</span><spanclass="token string"style=
<p>If your package calls for more fine-grained control over resolution priorities,
use a config pattern like this:</p>
<divclass="language-ts codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-ts codeBlock_bY9V thin-scrollbar"style="color:#393A34;background-color:#f6f8fa"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#393A34"><spanclass="token keyword"style="color:#00009f">interface</span><spanclass="token plain"></span><spanclass="token class-name">InterceptResolutionConfig</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"> abortPriority</span><spanclass="token operator"style="color:#393A34">?</span><spanclass="token operator"style="color:#393A34">:</span><spanclass="token plain"></span><spanclass="token builtin">number</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"> continuePriority</span><spanclass="token operator"style="color:#393A34">?</span><spanclass="token operator"style="color:#393A34">:</span><spanclass="token plain"></span><spanclass="token builtin">number</span><spanclass="token punctuation"style="color:#393A34">;</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">}</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"style="display:inline-block"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// This approach supports multiple priorities based on situational</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// differences. You could, for example, create a config that</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// allowed separate priorities for PNG vs JPG.</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">const</span><spanclass="token plain"></span><spanclass="token constant"style="color:#36acaa">DEFAULT_CONFIG</span><spanclass="token operator"style="color:#393A34">:</span><spanclass="token plain"> InterceptResolutionConfig </span><spanclass="token operator"style="color:#393A34">=</span><spanclass="token plain"></span><spanclass="token punctuation"style="color:#393A34">{</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"> abortPriority</span><spanclass="token operator"style="color:#393A34">:</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">undefined</span><spanclass="token punctuation"style="color:#393A34">,</span><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// Default to Legacy Mode</span><spanclass="token plain"></span><br></span><spanclass="token-line"style="color:#393A34"><spanclass="token plain"> continuePriority</span><spanclass="token operator"style="color:#393A34">:</span><spanclass="token plain"></span><spanclass="token keyword"style="color:#00009f">undefined</span><spanclass="token punctuation"style="color:#393A34">,</span><spanclass="token plain"></span><spanclass="token comment"style="color:#999988;font-style:italic">// Default to Legacy Mode</span><spanclass="token plain"></span><br></spa
<p>The above solutions ensure backward compatibility while also allowing the user