Description
Version
1.43.1
High-level description
See minimal repro code at: https://gist.github.com/jtbandes/039be5d61760aeb0eb12389228e643d1
-
This code uses
_electron.launch({...})
to run a small Electron app. It then installs a route handler usingawait electronApp.firstWindow().route("https://example.com/**", ...)
which responds with.fulfill({ json: { foo: "bar" } })
. -
The app's renderer process then invokes
fetch("https://example.com/foo")
and logs the response. -
When the app's main process uses
session.defaultSession.webRequest.onHeadersReceived
to modify response headers, the renderer can no longer read the mocked status and headers.
Steps to reproduce
-
Clone
git clone https://gist.github.com/039be5d61760aeb0eb12389228e643d1.git
and enter the cloned directory -
Run
npm install
-
Run
node index.js
-
Observe that the output from index.js indicates the mock response handler is called:
$ node index.js [main stderr] [36923:0423/172019.226754:ERROR:CONSOLE(1)] "Request Autofill.enable failed. {"code":-32601,"message":"'Autofill.enable' wasn't found"}", source: devtools://devtools/bundled/core/protocol_client/protocol_client.js (1) fulfilling mock request: https://example.com/foo [renderer log] got response: Response 0 false {} [renderer log] json: {foo: bar}
-
In the electron app that opens, notice that the logged Response has
status: 0
andok: false
, and the headers are empty:
When this happens, the request appears in the Network tab as "Finished" but the headers are not visible:
-
Open
main.js
and remove or comment out this sectionsession.defaultSession.webRequest.onHeadersReceived(...)
-
Re-run
node index.js
and observe thatstatus
,ok
, andheaders
now have the correct values:
The request also appears with the correct status and headers in the Network tab:
Expected behavior
session.webRequest.onHeadersReceived
is supposed to allow modifying response headers. It can be seen to work for real (non-mocked) requests:

Actual behavior
When session.webRequest.onHeadersReceived
is combined with mocked responses (via .route()
) it results in status: 0
, ok: false
, and headers: {}
. The status and headers returned by the mock implementation are lost.
Additional context
On rare occasions, it seems the .route()
mock does not work at all, and the request actually goes to the network. This seems to be somehow correlated with opening the dev tools, and when it happens there is additional log output [renderer error] Failed to load resource: net::ERR_INVALID_URL
. If you remove the mainWindow.webContents.openDevTools()
line, it seems to stop happening. It might be interesting to investigate why this happens but it's tangential to my reported issue.
Environment
System:
OS: macOS 14.4.1
CPU: (10) arm64 Apple M1 Pro
Memory: 152.30 MB / 16.00 GB
Binaries:
Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v18.18.2/bin/yarn
npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
pnpm: 8.10.5 - ~/.nvm/versions/node/v18.18.2/bin/pnpm
IDEs:
VSCode: 1.88.1 - /usr/local/bin/code
Languages:
Bash: 3.2.57 - /bin/bash
npmPackages:
playwright: 1.43.1 => 1.43.1