Skip to content
Permalink
Browse files
A stopped service worker fails to pass header and referrer on fetch r…
…equest

https://bugs.webkit.org/show_bug.cgi?id=242910
rdar://problem/97616866

Reviewed by Alex Christensen.

We can use the preoload if we try to load the same request or a cloned request.
We cannot in general reuse the preload if the request is changing a bit.
Remove the setting of the navigation preload identifier when creating a new request from another.

* LayoutTests/http/wpt/service-workers/fetch-service-worker-preload-changing-request.https-expected.txt: Added.
* LayoutTests/http/wpt/service-workers/fetch-service-worker-preload-changing-request.https.html: Added.
* LayoutTests/http/wpt/service-workers/fetch-service-worker-preload-worker.js:
(async event.event.request.url.includes.event.preloadResponse.async return):
* LayoutTests/http/wpt/service-workers/resources/fetch-service-worker-preload-script.py:
(main):
* Source/WebCore/Modules/fetch/FetchRequest.cpp:
(WebCore::FetchRequest::initializeWith):

Canonical link: https://commits.webkit.org/257110@main
  • Loading branch information
youennf committed Nov 29, 2022
1 parent cb01f4c commit 157a46485c50713332ff3eab7c3ae81a67abe448
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
@@ -0,0 +1,5 @@


PASS Setup activating worker
PASS Service worker load uses preload through calling fetch on the fetch event request

@@ -0,0 +1,67 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="resources/routines.js"></script>
</head>
<body>
<script>
var activeWorker;
var uuid = token();
var url = "/WebKit/service-workers/resources/fetch-service-worker-preload-script.py?getResponseFromRequestWithCustomHeader=yes&customHeader=1&token=" + uuid;
var frame;
const channel = new MessageChannel();

function waitUntilActivating()
{
return new Promise(resolve => {
channel.port2.onmessage = (event) => {
if (event.data === "activating")
resolve();
};
});
}

function triggerActivation()
{
activeWorker.postMessage("activate");
}

promise_test(async (test) => {
if (window.testRunner) {
testRunner.setUseSeparateServiceWorkerProcess(true);
await fetch("").then(() => { }, () => { });
}

let registration = await navigator.serviceWorker.register("/WebKit/service-workers/fetch-service-worker-preload-worker.js", { scope : url });
if (!registration.installing) {
registration.unregister();
registration = await navigator.serviceWorker.register("/WebKit/service-workers/fetch-service-worker-preload-worker.js", { scope : url });
}

activeWorker = registration.installing;
activeWorker.postMessage({ port: channel.port1 }, [channel.port1]);

return waitUntilActivating();
}, "Setup activating worker");

promise_test(async (test) => {
fetch(url + "&value=use-preload", { method: 'POST' });

// Load iframe, with activating worker, so only preload will start.
const promise = withIframe(url);

triggerActivation();

const frame = await promise;
assert_equals(frame.contentWindow.value, "my-custom-header");

// We should have only one GET fetch to url: the service worker preload
const response = await fetch(url + "&count=True");
assert_equals(await response.text(), "1");
}, "Service worker load uses preload through calling fetch on the fetch event request");
</script>
</body>
</html>
@@ -41,6 +41,13 @@ self.addEventListener('fetch', async (event) => {
return;
}

if (event.request.url.includes("getResponseFromRequestWithCustomHeader")) {
const newRequest = new Request(event.request, {
headers: { "x-custom-header": "my-custom-header" },
});
event.respondWith(fetch(newRequest));
return;
}

if (event.request.url.includes("getCloneResponseFromNavigationPreload") && event.preloadResponse) {
event.respondWith((async () => {
@@ -41,5 +41,8 @@ def main(request, response):
response.headers.set(b"Content-Type", b"text/vcard")
return value.decode()

if b"customHeader" in request.GET:
value = request.headers.get(b"x-custom-header", b"no custom header")

response.headers.set(b"Content-Type", b"text/html")
return "<html><body><script>window.value = '%s';</script></body></html>" % value.decode()
@@ -207,7 +207,6 @@ ExceptionOr<void> FetchRequest::initializeWith(FetchRequest& input, Init&& init)
{
m_request = input.m_request;
m_requestURL = m_request.url();
m_navigationPreloadIdentifier = input.navigationPreloadIdentifier();

m_options = input.m_options;
m_referrer = input.m_referrer;

0 comments on commit 157a464

Please sign in to comment.