How to bypass WebWorker/ServiceWorker requests in Puppeteer?

I’m working with Puppeteer version 18.0.5 and trying to intercept or bypass requests made by ServiceWorkers and WebWorkers. I found some older methods that don’t work with the current version.

In older Puppeteer versions, there was a _client property available:

page._client.send('Network.setBypassServiceWorker', {bypass: true})

Since my version doesn’t have the _client property, I tried this approach:

const session = await page.target().createCDPSession();
await session.send("Network.setBypassServiceWorker", { bypass: true });

Unfortunately, this method isn’t working for me. The ServiceWorker requests are still not being intercepted. Has anyone found a working solution for recent Puppeteer versions? I need to capture or block these background requests for my automation script.

You’re on the right track with CDP sessions, but handle it at the browser level, not page level. Use browser.target() instead of page.target() - gives you way more control over worker contexts. I banged my head against this for weeks before figuring out the bypass command has to be set globally before creating any pages. Also heads up - some sites register service workers the second they load, so even with perfect timing you might need to clear the browser’s service worker cache first with session.send('Storage.clearDataForOrigin'). Network.setBypassServiceWorker works but only stops new worker registrations, not existing ones.

Hit this exact issue a few months back upgrading from v15 to v20. It’s not the CDP session creation that’s broken - it’s timing and context. You’ve got to set up the bypass before the service worker registers. Here’s what fixed it for me: intercept the service worker registration by listening for ‘Network.requestWillBeSent’ events and block any requests with ‘serviceWorker’ or ‘webWorker’ in the initiator. Then handle the actual worker requests through the main thread context. Also, don’t forget to call session.send('Runtime.enable') with your Network.enable command. Service workers run in their own context separate from the main page, so your bypass needs to be active when the worker first loads - not just when it starts making requests.

Try enabling the CDP session before navigating to the page. I had the same issue and calling session.send('Network.enable') first made the bypass command work for me in v19. Just make sure you’re calling it before any page.goto()