Closed Bug 1876060 Opened 1 year ago Closed 7 months ago

Local script override doesn't work for SW cached request

Categories

(DevTools :: Debugger, defect, P2)

defect

Tracking

(firefox130 fixed)

RESOLVED FIXED
130 Branch
Tracking Status
firefox130 --- fixed

People

(Reporter: ochameau, Assigned: jdescottes)

References

(Blocks 2 open bugs)

Details

Attachments

(1 file)

STR:

AR:
analytics.js isn't overrided and instead the SW cached version is used.
Notice that the purple icon is displayed in the source tree, but the file isn't really overriden.

ER:
the override is used.

Note that if you disable service worker via dom.serviceWorkers.enabled set to false, the override works.

We actually do try to override the channel's response content... but it doesn't impact the actual content used.
The request do emit service-worker-synthesized-response notification
and we do call NetworkOverride.overrideChannelWithFilePath from there:
https://searchfox.org/mozilla-central/rev/734887cfb193b5697e59a857d394e8d4245996db/devtools/shared/network-observer/NetworkObserver.sys.mjs#371
but it doesn't work for some reason. I suspect that service-worker-synthesized-response is emitted too late.
(I don't see any particular assertion in debug build)

May be the override should be done before the call to mChannel->StartSynthesizedResponse() ?
https://searchfox.org/mozilla-central/rev/734887cfb193b5697e59a857d394e8d4245996db/dom/serviceworkers/ServiceWorkerEvents.cpp#353-365

Valentin, do you know if the service-worker-synthesized-response observer notification is a good spot to override the response?

Flags: needinfo?(valentin.gosu)

(In reply to Julian Descottes [:jdescottes] from comment #2)

Valentin, do you know if the service-worker-synthesized-response observer notification is a good spot to override the response?

I am not very familiar with how service worker channels work, but I think service-worker-synthesized-response might actually be too late.
If I understand correctly how SW work, the page attempts to load URL, nsHttpChannel calls ShouldIntercept and possibly redirects to the InteceptedHttpChannel, which then runs the SW. The SW will either provide the response from cache, or pass it through to the network via another channel.

If we want to override the contents of the JS file, I assume we want to do that before deciding whether to intercept? (please correct me if that's not the case)
If so, the notification we need to use to override the contents is http-on-before-connect.

Eden knows more about SW and might know if there's a better way to do this in SW code.

Flags: needinfo?(valentin.gosu) → needinfo?(echuang)

(In reply to Valentin Gosu [:valentin] (he/him) from comment #3)

If we want to override the contents of the JS file, I assume we want to do that before deciding whether to intercept? (please correct me if that's not the case)
If so, the notification we need to use to override the contents is http-on-before-connect.

This makes a lot of sense. Sounds worth investigating using http-on-before-connect for all overrides.

(In reply to Alexandre Poirot [:ochameau] from comment #4)

(In reply to Valentin Gosu [:valentin] (he/him) from comment #3)

If we want to override the contents of the JS file, I assume we want to do that before deciding whether to intercept? (please correct me if that's not the case)
If so, the notification we need to use to override the contents is http-on-before-connect.

This makes a lot of sense. Sounds worth investigating using http-on-before-connect for all overrides.

Quick note: we are trying to switch to an observer notification to create the network events a bit earlier in Bug 1849686. We initially tried to use http-on-before-connect, we are now trying with a new notification which :valentin is working on, emitted before dispatching the transaction. Although for blocking/rewriting purposes I guess http-on-before-connect probably works fine.

https://searchfox.org/mozilla-central/rev/cee2c396081d950f9e3401113fb179999e404ab8/dom/serviceworkers/FetchEventOpChild.cpp#528-538

According to codes, InterceptedHttpChannel starts synthesizing response and also start pump to the content, then sends service-worker-synthesized-response notification. So the service-worker-synthesized-response may not be a good point to override the content.

Flags: needinfo?(echuang)

Right now the new notification added in Bug 1849686 is blocked on a devtools service worker test issue. If needed we could just switch to http-on-before-connect only for the local script override, but for now let's try to wait for the new observer notification.

Severity: -- → S3
Depends on: 1849686
Priority: -- → P2
Depends on: 1880803
No longer depends on: 1849686

While I was testing migrating devtools' local override feature to use the Channel setResponseOverride API added in Bug 1900375, I also had to move handling the override to http-on-before-connect (because setResponseOverride only works if you call it before the request reaches the network).

It seems to fix this scenario, but only if we make sure that the request is not already in the service worker cache, because in that case we will not even get http-on-before-connect.

So you basically have to do a hard reload to make sure we fetch the override script. Given that setResponseOverride currently messes up with the netmonitor tracking, I think it will be best to only move the handling the overrides to http-on-before-connect for now.

Assignee: nobody → jdescottes
Status: NEW → ASSIGNED
Blocks: 1909840
Pushed by jdescottes@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/d14cbebffa0c [devtools] Handle local script override in http-on-before-connect r=devtools-reviewers,bomsy
Status: ASSIGNED → RESOLVED
Closed: 7 months ago
Resolution: --- → FIXED
Target Milestone: --- → 130 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: