Skip to content

Truly independent web browser

LibWeb: Keep a link's preload fetch going when it is fetched again

Andreas Kling54f964f0f190
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
index bb357bc06ab..cfc093c11fb 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp
@@ -467,6 +467,7 @@ void HTMLLinkElement::fetch_and_process_linked_resource()
if (m_fetch_controller) {
m_fetch_controller->stop_fetch();
+ m_fetch_controller = nullptr;
document().remove_from_script_blocking_style_sheet_set(*this);
}
@@ -610,14 +611,15 @@ void HTMLLinkElement::fetch_and_process_linked_preload_resource()
options->destination = destination.get<Optional<Fetch::Infrastructure::Request::Destination>>();
// 6. Preload options, with the following steps given a response response:
+ // NB: The preload's fetch is not stopped when el is fetched again, since other fetches may be waiting on it as a
+ // consumer of the preloaded resource.
GC::Weak weak_this { *this };
auto fetch_generation = m_current_fetch_generation;
- m_fetch_controller = preload(options, [weak_this, fetch_generation](Fetch::Infrastructure::Response& response) {
+ preload(options, [weak_this, fetch_generation](Fetch::Infrastructure::Response& response) {
// 1. If response is a network error, fire an event named error at el. Otherwise, fire an event named load at el.
if (auto link_element = weak_this.ptr()) {
if (fetch_generation != link_element->m_current_fetch_generation)
return;
- link_element->m_fetch_controller = nullptr;
if (response.is_network_error())
link_element->dispatch_event(create_event_for_element(*link_element, HTML::EventNames::error));
else
@@ -939,12 +941,12 @@ static bool type_matches_destination(Utf16View type, Optional<Fetch::Infrastruct
}
// https://html.spec.whatwg.org/multipage/links.html#preload
-GC::Ptr<Fetch::Infrastructure::FetchController> HTMLLinkElement::preload(LinkProcessingOptions& options, Function<void(Fetch::Infrastructure::Response&)> process_response)
+void HTMLLinkElement::preload(LinkProcessingOptions& options, Function<void(Fetch::Infrastructure::Response&)> process_response)
{
auto& realm = options.environment->realm();
// 1. If options's type doesn't match options's destination, then return.
if (!type_matches_destination(options.type, options.destination))
- return {};
+ return;
// 2. If options's destination is "image" and options's source set is not null, then set options's href to the
// result of selecting an image source from options's source set.
@@ -956,14 +958,14 @@ GC::Ptr<Fetch::Infrastructure::FetchController> HTMLLinkElement::preload(LinkPro
options.href = options.source_set->select_an_image_source(device_pixel_ratio).source.url;
}
if (options.href.is_empty())
- return {};
+ return;
// 3. Let request be the result of creating a link request given options.
auto request = create_link_request(options);
// 4. If request is null, then return.
if (!request)
- return {};
+ return;
// FIXME: 5. Let unsafeEndTime be 0.
@@ -1047,8 +1049,6 @@ GC::Ptr<Fetch::Infrastructure::FetchController> HTMLLinkElement::preload(LinkPro
options.on_document_ready = commit;
else
commit->function()(*options.document);
-
- return controller;
}
void HTMLLinkElement::modulepreload(LinkProcessingOptions const& options)
diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.h b/Libraries/LibWeb/HTML/HTMLLinkElement.h
index d77704cff0c..df3f0422550 100644
--- a/Libraries/LibWeb/HTML/HTMLLinkElement.h
+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.h
@@ -185,7 +185,7 @@ private:
bool stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request&);
static void preconnect(LinkProcessingOptions const&);
- static GC::Ptr<Fetch::Infrastructure::FetchController> preload(LinkProcessingOptions&, Function<void(Fetch::Infrastructure::Response&)> process_response = {});
+ static void preload(LinkProcessingOptions&, Function<void(Fetch::Infrastructure::Response&)> process_response = {});
static void modulepreload(LinkProcessingOptions const&);
void process_linked_resource(bool success, Fetch::Infrastructure::Response const&, CSS::StyleScope::OriginClean, Core::ImmutableBytes const*);
diff --git a/Tests/LibWeb/Text/expected/HTML/link-preload-consumer-stopped-while-waiting.txt b/Tests/LibWeb/Text/expected/HTML/link-preload-consumer-stopped-while-waiting.txt
new file mode 100644
index 00000000000..ef5eac6b0ba
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/link-preload-consumer-stopped-while-waiting.txt
@@ -0,0 +1,2 @@
+Stylesheet loaded: true
+Body color: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/expected/HTML/link-preload-switched-to-stylesheet-while-loading.txt b/Tests/LibWeb/Text/expected/HTML/link-preload-switched-to-stylesheet-while-loading.txt
new file mode 100644
index 00000000000..ef5eac6b0ba
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/link-preload-switched-to-stylesheet-while-loading.txt
@@ -0,0 +1,2 @@
+Stylesheet loaded: true
+Body color: rgb(0, 128, 0)
diff --git a/Tests/LibWeb/Text/input/HTML/link-preload-consumer-stopped-while-waiting.html b/Tests/LibWeb/Text/input/HTML/link-preload-consumer-stopped-while-waiting.html
new file mode 100644
index 00000000000..314a650e98a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-preload-consumer-stopped-while-waiting.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const token = `link-preload-consumer-stopped-while-waiting-${Math.random()}`;
+ const preloadURL = await server.createEcho("GET", "/link-preload-consumer-stopped-while-waiting-preload", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: "body { color: rgb(255, 0, 0); }",
+ wait_for_unblock: token,
+ });
+ const stylesheetURL = await server.createEcho("GET", "/link-preload-consumer-stopped-while-waiting-stylesheet", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: "body { color: rgb(0, 128, 0); }",
+ });
+
+ const link = document.createElement("link");
+ link.rel = "preload";
+ link.as = "style";
+ link.href = preloadURL;
+ document.head.append(link);
+
+ // Make sure the preload request reached the server, which holds its response until unblocked.
+ const recordedURL = server.recordedRequestHeadersURL(preloadURL);
+ while (true) {
+ try {
+ if ((await fetch(recordedURL)).ok) break;
+ } catch {}
+ }
+
+ // The stylesheet fetch waits on the preload that is still in flight, and is then stopped by the href change.
+ link.rel = "stylesheet";
+ link.href = stylesheetURL;
+ const loaded = new Promise(resolve => link.addEventListener("load", resolve, { once: true }));
+
+ // Let the preload complete while nothing waits on it anymore.
+ await fetch(new URL(`/unblock/${token}`, preloadURL));
+ await loaded;
+ await fetch(preloadURL, { mode: "no-cors" });
+ println(`Stylesheet loaded: ${!!link.sheet}`);
+ println(`Body color: ${getComputedStyle(document.body).color}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/link-preload-switched-to-stylesheet-while-loading.html b/Tests/LibWeb/Text/input/HTML/link-preload-switched-to-stylesheet-while-loading.html
new file mode 100644
index 00000000000..8cd7f2f7460
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-preload-switched-to-stylesheet-while-loading.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const server = httpTestServer();
+ const token = `link-preload-switched-to-stylesheet-while-loading-${Math.random()}`;
+ const url = await server.createEcho("GET", "/link-preload-switched-to-stylesheet-while-loading", {
+ status: 200,
+ headers: { "Content-Type": "text/css" },
+ body: "body { color: rgb(0, 128, 0); }",
+ wait_for_unblock: token,
+ });
+
+ const link = document.createElement("link");
+ link.rel = "preload";
+ link.as = "style";
+ link.href = url;
+ document.head.append(link);
+
+ // Make sure the preload request reached the server, which holds its response until unblocked.
+ const recordedURL = server.recordedRequestHeadersURL(url);
+ while (true) {
+ try {
+ if ((await fetch(recordedURL)).ok) break;
+ } catch {}
+ }
+
+ // The stylesheet fetch consumes the preload that is still in flight.
+ link.rel = "stylesheet";
+ const loaded = new Promise(resolve => link.addEventListener("load", resolve, { once: true }));
+
+ await fetch(new URL(`/unblock/${token}`, url));
+ await loaded;
+ println(`Stylesheet loaded: ${!!link.sheet}`);
+ println(`Body color: ${getComputedStyle(document.body).color}`);
+ done();
+ });
+</script>