question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Network requests in Workers

See original GitHub issue

WebWorkers can fetch data; we should surface traffic from web workers in puppeteer.

This is blocked on #2548 since nested targets are broken with request interception. See https://github.com/GoogleChrome/puppeteer/pull/2717#issuecomment-398899616 for details.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
mccolljrcommented, Oct 4, 2019

It’s now October 2019, I take it this is still not enabled? Is there an ETA?

7reactions
JBoudMScommented, Jul 19, 2019

@aslushnikov

Regarding the workaround, you can attach raw devtools protocol session with target.createCDPSession() and enable network domain manually there. It should give you some events.

Could you elaborate on this: enable network domain manually there.?

Not sure if it would still be useful 3 months later, but in case anybody else finds it useful…

This is based on https://jarrodoverson.com/post/using-chrome-devtools-protocol-with-puppeteer-737a1300bac0/ but updated to use Fetch as Network has been deprecated. It is working with "puppeteer": "^1.18.1"

/**
 * Captures all traffic including from Web Workers, does something with it, and continues the request
 * @param page The page/tab to capture from.
 */
const interceptAllTrafficForPageUsingFetch = async (page) => {
    if (page) {
        const client = await page.target().createCDPSession();
        //see: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-enable
        await client.send('Fetch.enable', {
            //see: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#type-RequestPattern
            patterns: [{
                urlPattern: '*',
                requestStage: 'Response'
            }]
        });
        //see: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#event-requestPaused
        await client.on('Fetch.requestPaused', async ({ requestId, request, responseHeaders }) => {
            const { url } = request;
            console.log(`Intercepted ${request.url}`)
            //see: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-getResponseBody
            const responseBody = await client.send('Fetch.getResponseBody', {
                requestId
            });

            //Do something like saving the response to disk

            //see: https://chromedevtools.github.io/devtools-protocol/tot/Fetch#method-continueRequest
            await client.send('Fetch.continueRequest', {
                requestId
            })
        })
    }
}
/**
 * Launches a puppeteer controlled Chrome and intercepts all traffic.
 */
(async () => {
     const browser = await launch({
        headless: false,
        defaultViewport: VIEWPORT,
    });
    const page = (await browser.pages())[0];

    await interceptAllTrafficForPageUsingFetch(page);
    browser.on('targetcreated', async (target) => {
        const page = await target.page();
        await interceptAllTrafficForPageUsingFetch(page);
    });

   //Normal code like navigation, closing browser session, etc.
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Service Workers to manage network requests and push ...
Service Workers are a special type of Web Worker with the ability to intercept, modify, and respond to all network requests using the...
Read more >
Using Service Workers - Web APIs | MDN
A service worker functions like a proxy server, allowing you to modify requests and responses replacing them with items from its own cache....
Read more >
Chapter 4. Intercepting network requests - Progressive Web ...
A Service Worker's ability to intercept any outgoing HTTP requests is what makes it so powerful. Every HTTP request that falls within this...
Read more >
Service workers - web.dev
That means the first time a user comes to your PWA, network requests will go directly to your server because the service worker...
Read more >
Making Service Workers easier to debug for Progressive Web ...
Step into and out of fetch event handlers with page script information. Inspecting network requests with the Edge DevTools. Try Service Worker ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found