Skip to content

Truly independent web browser

LibWeb: Do not consume a preload whose fetch was stopped

Andreas Kling61e1fe25c454
diff --git a/Libraries/LibWeb/HTML/PreloadEntry.cpp b/Libraries/LibWeb/HTML/PreloadEntry.cpp
index 168757ddf0c..70ae87531a9 100644
--- a/Libraries/LibWeb/HTML/PreloadEntry.cpp
+++ b/Libraries/LibWeb/HTML/PreloadEntry.cpp
@@ -106,6 +106,12 @@ bool consume_a_preloaded_resource(
// 8. Remove preloads[key].
preloads.remove(it);
+ // AD-HOC: A preload whose fetch was stopped before its response arrived, such as by stopping the document, will
+ // never have a response: Stopping a fetch drops its algorithms. Return false, so the consumer fetches on
+ // its own instead of waiting forever.
+ if (!entry->response && entry->controller && entry->controller->state() == Fetch::Infrastructure::FetchController::State::Stopped)
+ return false;
+
// AD-HOC: A preload reports its own timing to Resource Timing, but a speculative fetch does not. Let the consumer
// know, so that it only reports timing when the preload does not.
preload_reports_timing = !entry->is_speculative;
diff --git a/Tests/LibWeb/Text/expected/HTML/link-stylesheet-after-preload-stopped-by-window-stop.txt b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-after-preload-stopped-by-window-stop.txt
new file mode 100644
index 00000000000..2e702106f60
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/link-stylesheet-after-preload-stopped-by-window-stop.txt
@@ -0,0 +1 @@
+Loaded stylesheet, sheet: true
diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html b/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html
new file mode 100644
index 00000000000..44bfaff575a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const preload = document.createElement("link");
+ preload.rel = "preload";
+ preload.as = "style";
+ preload.href = "../../data/import-1.css";
+ document.head.appendChild(preload);
+
+ // Stopping the document stops the preload's fetch before it has a response.
+ window.stop();
+
+ const stylesheet = document.createElement("link");
+ stylesheet.rel = "stylesheet";
+ stylesheet.href = "../../data/import-1.css";
+ stylesheet.addEventListener("load", () => {
+ println(`Loaded stylesheet, sheet: ${stylesheet.sheet !== null}`);
+ done();
+ });
+ stylesheet.addEventListener("error", () => {
+ println("Error");
+ done();
+ });
+ document.head.appendChild(stylesheet);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html.headers b/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/link-stylesheet-after-preload-stopped-by-window-stop.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html