gecko-dev/dom/serviceworkers/test/opaque_intercept_worker.js
Blake Kaplan 19781c9ae7 Bug 1517406 - Resolve a race between the controlled iframe and the SW. r=asuth
Summary:

This test first creates a service worker and then an iframe. It expects the
iframe's first load to be through the network (and for the page's script to
fail to load). Once the page is set up and controlled by the SW, it reloads it
(a soft reload) and the script load is intercepted by the service worker and
we load a script that calls back into the test. However, there is currently no
guarantee that the service worker won't activate before the iframe load and
end up controlling the first load of the iframe.

With this patch, we delay the service worker's activation until we start
loading the iframe, allowing the first load to always be uncontrolled, thereby
resolving the race.

Reviewers: asuth

Reviewed By: asuth

Bug #: 1517406

Differential Revision: https://phabricator.services.mozilla.com/D15637

--HG--
extra : rebase_source : ff63b131b1a395289cda85ec4b46daa3022b08b4
2019-01-29 15:41:10 -05:00

32 lines
934 B
JavaScript

var name = 'opaqueInterceptCache';
// Cross origin request to ensure that an opaque response is used
var prefix = 'http://example.com/tests/dom/serviceworkers/test/'
var testReady = new Promise(resolve => {
self.addEventListener('message', (m) => {
resolve();
}, { once: true });
});
self.addEventListener('install', function(event) {
var request = new Request(prefix + 'notify_loaded.js', { mode: 'no-cors' });
event.waitUntil(
Promise.all([caches.open(name), fetch(request), testReady]).then(function(results) {
var cache = results[0];
var response = results[1];
return cache.put('./sw_clients/does_not_exist.js', response);
})
);
});
self.addEventListener('fetch', function (event) {
event.respondWith(
caches.open(name).then(function(cache) {
return cache.match(event.request);
}).then(function(response) {
return response || fetch(event.request);
})
);
});