catchpoint / WebPageTest

Official repository for WebPageTest
Other
3.08k stars 715 forks source link

could not block service worker js request #1228

Closed joychester closed 4 years ago

joychester commented 5 years ago

I am trying to block service worker javascript request with Webpagetest, but failed. Please double check if this is an issue with webpagetest. how could i block the service worker js during the tests?

here is a sample test for the reference: https://www.webpagetest.org/result/181212_QZ_e85d807f1493e2a15312dd0bfffe5fcc/1/details/#waterfall_view_step1

gilbertococchi commented 4 years ago

@pmeenan Do you know perhaps some workaround to block SWs somehow?

I never managed to make Request Blocking to work on a Service Worker file for some reason...

pmeenan commented 4 years ago

No. There is no way to block them. The service worker requests are not exposed in the dev tools interception hooks (at least as things stand today - It's possible I'm not holding something correctly with the various targets).

Since they are from the same domain as the origin there is also no way to block them at a DNS level.

joychester commented 4 years ago

@pmeenan ,may not need to block them but to disable that particular service worker, is that possible from webpagetest to do so with Chrome browser?

gilbertococchi commented 4 years ago

@verlok

Hi Patrick and Cheng, sorry for not being clear here. In my case the ask is not to be able to block the Service Worker "Requests" but the Service Worker file itself to prevent the registration of the Service Worker in the first place.

I believe doing so it would help to better evaluate the benefits of Service Worker precaching when navigating one page, waiting for the SW to install and precache resources and then browse to another page.

Currently it's not possible to block the SW file itself from being available from registration, here's an example of the possible result https://docs.google.com/presentation/d/1v1h9OyfOZEvPPi7sW1zsAQAA6BpFaXDCvc9AdsMmAtQ/edit#slide=id.g63045c29c3_0_377

logData 0 navigate https://www.chloe.com/ca?device=smartphone sleep 30 logData 1 navigate https://www.chloe.com/ca/chloe/shop-online/women/new-arrivals

when trying to block the file https://www.chloe.com/SW_CHLOE.js?siteCode=CHLOE_CA WPT fails to block that from what I can see.

I don't think there is a need to intercept requests going to the SW directly and you are right it's in another scope.

andydavies commented 4 years ago

This script seems to block the service worker file OK

block   SW_Registration.js
navigate    https://www.chloe.com/ca?device=smartphone
sleep   30
navigate    https://www.chloe.com/ca/chloe/shop-online/women/new-arrivals

https://www.webpagetest.org/result/200117_8W_9f22401b4413ebfa6f3b8ec8cbeed035

gilbertococchi commented 4 years ago

Hi Andy, good catch, maybe I've raised the wrong example here :) They script you posted works because there is an additional external JS file to handle the SW registration SW_Registration.js.

If I try to block the Service Worker file itself it doesn't seem to work in a site where there is no this additional layer, example:

https://webpagetest.org/result/200117_SG_73878a531f7685bbf6c693b5da9c7ee9/1/details/#step1_request46

block sw.js navigate https://www.yoox.com/us/women/clothing%20new%20arrivals/shoponline sleep 40 execAndWait document.querySelectorAll('div.itemContainer a')[0].click()

andydavies commented 4 years ago

You can replace navigator.serviceWorker.register using script injection (bottom of the advanced tab)

Use something like

navigator.serviceWorker.register = function() {}
performance.mark("customScript");

(mark is only there so I could check the script ran as I've had not run sometimes)

Comparing step two of this and the original I think it works OK?

https://www.webpagetest.org/result/200117_YZ_6a4f90d23119c380a0847136602ca3d5/

pmeenan commented 4 years ago

I just extended the request blocking and interception to the worker targets. It will take an hour or so for the update to roll out.

btw, the worker is SW_CHLOE.js (not sw.js), at least in my testing.

pmeenan commented 4 years ago

Should be working now: https://www.webpagetest.org/result/200117_YF_9ff02082457319b5a663a86ffdaf7e87/

joychester commented 4 years ago

Thank for all help and figure out the way to resolve this one!! it works from my side too! Close this issue now!

gilbertococchi commented 4 years ago

Confirmed it works now!, Thank you so much Patrick!!