Requests are not intercepted when cached by Service Worker
See original GitHub issueNot 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();
- Set
await page.setRequestInterception(true)
- Run test, only URLs not previously cached by service worker are logged
- Set
await page.setRequestInterception(false)
(or remove Service Worker registration) - 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:
- Created 5 years ago
- Reactions:5
- Comments:12 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@angus-c we decided not to land #4095.
Summary: yes, requests are not intercepted when they come to service worker. Ideally, we want:
serviceWorker.on('request')
,serviceWorker.on('response')
,serviceWorker.setRequestInterception(true)
.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:
What’s the purpose of have a
HTTPResponse.fromServiceWorker()
method if it won’t work when request interception is settrue
?