webRequest.onResponseStarted event is not always fired


(Mateusz Marzantowicz) #1

Hello,

I’m writing simple add on that uses WebExtensions API. Currently I have one background script that registers listeners for various webRequest events (onBeforeRequest and onResponseStarted).

When visiting website (html + css or js) it sometimes happens, that onBeforeRequest is fired for all request types: main_frame, stylesheet, etc. but no corresponding onResponseStarted event is fired for request of typemain_frame. When I reload the same page all events are fired for given url as documented at https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest. It looks a bit random to me but this may only be my impression.

Is that expected that not all events are fired at various stages of making an HTTP request for certain request types like main_frame or is it a bug? Where can I find more info about when exactly onResponseStarted would not be fired?

Here is my sample code from background.js script

const requestFilter = { urls: ['<all_urls>'] }

browser.webRequest.onBeforeRequest.addListener(details => {
  if (details.type === 'main_frame') {
    console.log(`onBeforeRequest: ${details.url}`)
  }
}, requestFilter)

browser.webRequest.onResponseStarted.addListener(details => {
  if (details.type === 'main_frame') {
    console.log(`onResponseStarted: ${details.url}`)
  }
}, requestFilter)

(Martin Giger) #2

I could imagine this being cache related, but that’s just a hunch.


(Mateusz Marzantowicz) #3

It might be, but I can’t track any reasonable dependencies. It looks like onResponseStarted is never fired for main_frame when URL was typed in address bar or chosen from it. Only when I reload page it goes as expected: onBeforeRequest then onResponseStarted. Strange thing is that for other resource types I always see events fired in right order.


(erosman) #4

Also

https://bugzilla.mozilla.org/show_bug.cgi?id=1430479