Repository navigation
feat: add Service Worker network interception - #12565
Ross Wollman (rwoll) wants to merge 27 commits into
Conversation
|
Thanks |
Dmitry Gozman (dgozman)
left a comment
There was a problem hiding this comment.
Looks nice overall!
| const page = this.request().frame()._page; | ||
| // FIXME(raw): SW requests do not have an associated frame: we probably want to signal and check it is explicilty SW request | ||
| let page = null; | ||
| try { |
There was a problem hiding this comment.
const page = this.request()._frame?._page;
|
adding a method on the service-worker's request/response that references the page/worker request would be helpful as well here. as can be seen in the devtools here https://googlechrome.github.io/samples/service-worker/basic/ |
|
Is there a general ETA on this work. This is crucial to an issue im solving now. If this is slated for a later release, I'll move in a different direction for the time being. No pressure, just curious. |
Doug Bowen (@Doug-Bowen) Thanks for your interest! I can't commit to an ETA. Please subscribe to #1090 to be notified of when it lands. Once it's in the main branch, you'll be able to try it out via |
86aa2d2 to
1baab20
Compare
This is the beginning of fixing microsoft#1090. There's still quite a bit to be done, but this at least shows an end-to-end path to accomplish the feature.
There's still lots that needs to be changed and cleaned up. Initial attempts tried to get too much working at once (and create pretty but possibly poor abstractions), so this backs off the changes to a minimal set. NB: This patchset requires a CR >= r979924 (https://chromium-review.googlesource.com/c/chromium/src/+/3510917). To use a local copy, be sure to set `CRPATH` like so: ``` $ CRPATH=$HOME/code/google/chromium/src/out/Default/Chromium.app/Contents/MacOS/Chromium npm run ctest -- --reporter=line ```
1baab20 to
765ed30
Compare
| if (!event.responseStatusCode && !event.responseErrorReason) { | ||
| // Request intercepted, deliver signal to the tracker. | ||
| const request = this._requestIdToRequest.get(event.networkId!); | ||
| const request = this._requestIdToRequest.get(event.networkId!) || this._maybeAdoptServiceWorkerMainRequest(event.networkId!); |
There was a problem hiding this comment.
Add a test that 'request' event is dispatched for the main sw script without interception, and is not dispatched on the page.
|
Addressed the mechanical fixes/improvements. Will address the This is getting near feature complete for an initial land modulo some additional testing. 😄 |
| expect(await worker.evaluate(() => self.toString())).toBe('[object ServiceWorkerGlobalScope]'); | ||
| }); | ||
|
|
||
| test('should create a worker from service worker with noop routing', async ({ context, page, server }) => { |
There was a problem hiding this comment.
I think we should also verify that page does not report the requests sent from within the service worker, only the request being sent from the page (being intercepted by the service-worker)
There was a problem hiding this comment.
I think we should also verify that page does not report the requests sent from within the service worker, only the request being sent from the page (being intercepted by the service-worker)
Meir Blachman (@Meir017) Let me make sure I'm following!
So if example.html looks like:
<script>
navigator.serviceWorker.register('/worker.js');
navigator.serviceWorker.ready.then(async registration => {
await fetch('/payload.txt');
});
</script>and worker.js looks like:
fetch('/top-level-sw-request.txt');
self.addEventListener('fetch', event => {
if (!event.request.url.endsWith('/payload.txt')) return;
event.respondWith(fetch(event.request));
});On which Playwright object (page or serviceWorker) would you expect to see the request event fire for each of the two requests (/payload.txt and /top-level-sw-request.txt)?
There was a problem hiding this comment.
I would expect to see two request events fired. one for the page and another for the service worker.
this can be seen in the network tab of the browser.
from your example
- page request
/worker.js - sw request
/top-level-sw-request.txt - page request
/payload.txt - sw request
/payload.txt
see my comment #12565 (comment)
| // Service Worker Network.requestWillBeSent event fires on the parent frame session, | ||
| // but the Fetch.requestPaused comes in on the Service Worker session. | ||
| // We assume these came in the the same session (on the Service Worker), so shuffle things around. | ||
| // In the future, this may be resolved; see https://crbug.com/1304536 |
There was a problem hiding this comment.
Currently fixing this upstream.
Depends on https://chromium-review.googlesource.com/c/chromium/src/+/3526571 Set CRPATH in .env before running.
|
Closing this! Will re-open once CR upstream changes are all landed and this code is reworked a bit around some internal refactors and conflicts. |



Resolves #1090.
Some known TODOs:
this._page || this._serviceWorkerbitsFIXME(raw):/TODO(raw):request.frame()is always valid…it was before!)(maybe) Addworker.on('request',...)etc. events to mirror page events