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.

Requests are not intercepted when cached by Service Worker

See original GitHub issue

Not sure if this is by design but requests that were previously cached by service worker are not intercepted after page.setRequestInterception(true).

Simplified code to reproduce:

const hostname = 'http://localhost:';
const {port, proc} = await startServer(); // local implementation
const browser = await puppeteer.launch({
  args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
    '--enable-features=NetworkService',
  ],
  ignoreHTTPSErrors: true,
});

const page = await browser.newPage();

await page.goto(`${hostname}${port}`);
await page.evaluate('navigator.serviceWorker.ready');

await page.setRequestInterception(true); //toggle this
page.on('request', req => {
   console.log('requesting', req.url());
   req.continue().catch(e => e /* not intercepting */);
});

await page.reload({waitUntil: 'domcontentloaded'});

await browser.close();
  1. Set await page.setRequestInterception(true)
  2. Run test, only URLs not previously cached by service worker are logged
  3. Set await page.setRequestInterception(false) (or remove Service Worker registration)
  4. Run test, all URLs are logged

Are requests intercepted only when requests are sent through to the network, or is this a bug?

Issue Analytics

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

github_iconTop GitHub Comments

20reactions
aslushnikovcommented, Mar 5, 2019

@angus-c we decided not to land #4095.

Summary: yes, requests are not intercepted when they come to service worker. Ideally, we want:

  • connect to page’s service worker
  • expose ServiceWorker network API similar to the page’s one: serviceWorker.on('request'), serviceWorker.on('response'), serviceWorker.setRequestInterception(true).
  • page’s network events should relate to all network requests initiated by page
  • serviceWorker network events should relate to all network requests initiated by SW

While we don’t have this implemented, Puppeteer simply doesn’t fire any request events if they are captured by SW and request interception is enabled. If this is a no-go, one can use DevTools protocol to enable SW by-passing for network:

page._client.send('Network.setBypassServiceWorker', {bypass: true})
2reactions
JulianSotocommented, Mar 20, 2021

What’s the purpose of have a HTTPResponse.fromServiceWorker() method if it won’t work when request interception is set true?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requests are not intercepted when cached by Service Worker
Summary: yes, requests are not intercepted when they come to service worker. Ideally, we want: connect to page's service worker; expose ...
Read more >
ServiceWorker not intercepting URLs - Stack Overflow
If I omit the scope, the SW will then intercept all requests, but I only want to intercept requests to /boot/app/3/ . I...
Read more >
Chapter 4. Intercepting network requests - Progressive Web ...
Instead of the user making a request to the server, the Service Worker intercepts the request and decides to serve it from cache...
Read more >
Sync XHR does not get intercepted by service worker - Monorail
If you force your computer offline and issue a sync xhr request to a cached service worker route it will fail. However if...
Read more >
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 >

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