Skip to content

Lightpanda: the headless browser designed for AI and automation

frame: commit an error document when a root navigation fails

Scott Taylor6d4adb0f7abd
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 20b8eddea..288c0340b 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -1995,13 +1995,26 @@ fn frameErrorCallback(ctx: *anyopaque, err: anyerror) void {
});
}
- // A pending root navigation that failed before commit: discard the
- // pending Page; the OLD active Page (and its V8 context) is untouched.
- // We do NOT run frameDoneCallback against the pending frame — the frame
- // is about to be freed.
+ // A pending root navigation that failed before commit. A cancelled one
+ // (window.stop(), Fetch.failRequest) leaves the OLD active Page untouched.
+ // Otherwise, like Chrome, commit an error document in its place: the OLD
+ // document was superseded (see abortDocumentLoad) and will never fire
+ // DOMContentLoaded or load.
if (self.page.replaces != null) {
- self._session.discardPendingPage(self.page);
- return;
+ if (err == error.TransferCanceled) {
+ self._session.discardPendingPage(self.page);
+ return;
+ }
+ self._session.commitPendingPage(self.page) catch |e| {
+ log.err(.frame, "commit error page", .{ .err = e, .type = self._type, .url = self.url });
+ if (self.page.replaces != null) {
+ self._session.discardPendingPage(self.page);
+ }
+ return;
+ };
+ self.errorPageNavigated() catch |e| {
+ log.err(.frame, "error page navigated", .{ .err = e, .type = self._type, .url = self.url });
+ };
}
self._parse_state.deinit(self);
@@ -2015,6 +2028,31 @@ fn frameErrorCallback(ctx: *anyopaque, err: anyerror) void {
};
}
+// The parts of frameHeaderDoneCallback an error document committed in place of
+// a pending root navigation needs. Like Chrome's error page, its origin is
+// opaque.
+fn errorPageNavigated(self: *Frame) !void {
+ self.origin = null;
+ try self.js.setOrigin(null);
+
+ const location = try Location.init(self.url, self);
+ location.acquireRef();
+ self.window._location.releaseRef(self.page);
+ self.window._location = location;
+
+ var opts = self._navigated_options orelse return;
+ // frame_navigate_failed already answered the Page.navigate command.
+ opts.cdp_id = null;
+ self._session.notification.dispatch(.frame_navigated, &.{
+ .opts = opts,
+ .url = self.url,
+ .req_id = self._req_id,
+ .frame_id = self._frame_id,
+ .loader_id = self._loader_id,
+ .timestamp = lp.datetime.timestamp(.boot),
+ });
+}
+
pub fn isGoingAway(self: *const Frame) bool {
return self.js.env.terminatePending() or self.hasQueuedNavigation();
}
@@ -4124,6 +4162,22 @@ test "Frame: pending or discarded replacements do not resume old load events" {
}
}
+test "Frame: a failed navigation commits an error document over the superseded one" {
+ testing.silenceLog(&.{.frame});
+ const page = try testing.pageTest("fixtures/navigation_failed.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ try testing.expectEqualSlices(u8, "http://127.0.0.1:1/unreachable", frame.url);
+ try testing.expectEqual(.complete, frame._load_state);
+
+ var ls: JS.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const state = try ls.local.exec("document.readyState + '|' + document.querySelector('h1')?.textContent + '|' + document.querySelector('p')?.textContent", null);
+ try testing.expectEqual("complete|Navigation failed|Reason: CouldntConnect", try state.toStringSlice());
+}
+
test "Frame: readystatechange during an aborted load may renavigate or throw" {
const page = try testing.pageTest("hi.html", .{});
defer page.close();
diff --git a/src/browser/tests/fixtures/navigation_failed.html b/src/browser/tests/fixtures/navigation_failed.html
new file mode 100644
index 000000000..11fdc499a
--- /dev/null
+++ b/src/browser/tests/fixtures/navigation_failed.html
@@ -0,0 +1,6 @@
+<!doctype html>
+<title>Original</title>
+<script>
+location.assign('http://127.0.0.1:1/unreachable');
+</script>
+<p>superseded</p>
diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zig
index c9ddf74a9..5c6a99cfd 100644
--- a/src/server/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -2402,15 +2402,58 @@ test "cdp.frame: navigate answers with errorText when the navigation fails" {
});
try testing.waitForPage(bc);
- // The pending page was discarded; the active document is untouched.
- const frame = bc.mainFrame() orelse unreachable;
- try testing.expectEqualSlices(u8, "http://127.0.0.1:9582/src/browser/tests/hi.html", frame.url);
-
try ctx.expectSentResult(.{
.frameId = "FID-0000000001",
.loaderId = "LID-0000000002",
.errorText = "CouldntConnect",
}, .{ .id = 52 });
+
+ // As in Chrome, an error document replaces the active one, and committing
+ // it doesn't answer the command a second time.
+ try ctx.expectSentEvent("Page.frameNavigated", .{ .frame = .{ .url = "http://127.0.0.1:1/unreachable", .loaderId = "LID-0000000002" } }, .{});
+ const frame = bc.mainFrame() orelse unreachable;
+ try testing.expectEqualSlices(u8, "http://127.0.0.1:1/unreachable", frame.url);
+
+ var answers: usize = 0;
+ for (ctx.received.items) |msg| {
+ const msg_id = msg.object.get("id") orelse continue;
+ if (msg_id == .integer and msg_id.integer == 52) answers += 1;
+ }
+ try testing.expectEqual(1, answers);
+}
+
+test "cdp.frame: a failed script navigation commits an error document" {
+ testing.silenceLog(&.{.frame});
+
+ // A script navigation supersedes the current document, which then never
+ // fires DOMContentLoaded or load. If the navigation fails, clients waiting
+ // on lifecycle events only see them from the error document.
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-SNF", .session_id = "SID-SNF", .url = "hi.html", .target_id = "FID-0000000SNF".* });
+ try ctx.processMessage(.{ .id = 1, .method = "Page.setLifecycleEventsEnabled", .sessionId = "SID-SNF", .params = .{ .enabled = true } });
+
+ {
+ const frame = bc.mainFrame() orelse unreachable;
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec("location.assign('http://127.0.0.1:1/unreachable')", null);
+ }
+ // Outside the scope: the navigation destroys the page it's entered on.
+ try testing.waitForPage(bc);
+
+ try ctx.expectSentEvent("Page.frameNavigated", .{ .frame = .{ .url = "http://127.0.0.1:1/unreachable", .loaderId = "LID-0000000002" } }, .{ .session_id = "SID-SNF" });
+ try ctx.expectSentEvent("Page.lifecycleEvent", .{ .name = "DOMContentLoaded", .loaderId = "LID-0000000002" }, .{ .session_id = "SID-SNF" });
+ try ctx.expectSentEvent("Page.lifecycleEvent", .{ .name = "load", .loaderId = "LID-0000000002" }, .{ .session_id = "SID-SNF" });
+
+ const frame = bc.mainFrame() orelse unreachable;
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const v = try ls.local.exec("document.readyState === 'complete' && document.querySelector('h1')?.textContent === 'Navigation failed' && location.href === 'http://127.0.0.1:1/unreachable'", null);
+ try testing.expect(v.toBool());
}
test "cdp.frame: navigate to about:blank replaces a non-blank document" {