Skip to content

Lightpanda: the headless browser designed for AI and automation

Add condition to full page reload on about:blank from CDP

Karl Seguin5f96fc1c3cc4
diff --git a/src/cdp/CDP.zig b/src/cdp/CDP.zig
index 9399a7821..0d4596b92 100644
--- a/src/cdp/CDP.zig
+++ b/src/cdp/CDP.zig
@@ -578,6 +578,11 @@ pub const BrowserContext = struct {
inspector_session: *js.Inspector.Session,
isolated_worlds: std.ArrayList(*IsolatedWorld),
+ // True when Runtime.evaluate has been run in the main world. Optimization
+ // so that, if it has _not_ and the page is on its initial about:blank, we
+ // can use the existing Frame/js.Context as-is.
+ main_world_touched: bool = false,
+
// Scripts registered via Page.addScriptToEvaluateOnNewDocument.
// Evaluated in each new document after navigation completes.
scripts_on_new_document: std.ArrayList(ScriptOnNewDocument) = .empty,
diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig
index 88a617c84..15ae70a0a 100644
--- a/src/cdp/domains/page.zig
+++ b/src/cdp/domains/page.zig
@@ -300,13 +300,23 @@ fn navigate(cmd: *CDP.Command) !void {
const encoded_url = try URL.resolveNavigation(frame.call_arena, params.url, .{});
- // Always do a full navigate to clear any state (e.g. vs Runtime.evaluate)
- // that might have been built-up in a dummy about:blank page
- try session.initiateRootNavigation(frame._frame_id, encoded_url, .{
+ const opts: Frame.NavigateOpts = .{
.reason = .address_bar,
.cdp_id = cmd.input.id,
.kind = .{ .push = null },
- });
+ };
+
+ if (canNavigateInPlace(bc, frame)) {
+ return frame.navigate(encoded_url, opts);
+ }
+ try session.initiateRootNavigation(frame._frame_id, encoded_url, opts);
+}
+
+// Fast path that allows using the initial about:blank Frame as-is. Only safe
+// when the frame is still waiting for a navigate AND no JS has been run in the
+// instance (for about:blank, that's only possible via an Runtime.* call)
+fn canNavigateInPlace(bc: *const CDP.BrowserContext, frame: *const Frame) bool {
+ return frame._load_state == .waiting and !bc.main_world_touched;
}
fn doReload(cmd: *CDP.Command) !void {
@@ -422,7 +432,7 @@ fn navigateToHistoryEntry(cmd: *CDP.Command) !void {
.kind = .{ .traverse = idx },
};
- if (frame._load_state == .waiting) {
+ if (canNavigateInPlace(bc, frame)) {
return frame.navigate(url, opts);
}
@@ -525,6 +535,7 @@ pub fn frameCreated(bc: *CDP.BrowserContext, frame: *Frame) !void {
if (!in_commit) {
_ = bc.cdp.frame_arena.reset(.{ .retain_with_limit = 1024 * 512 });
+ bc.main_world_touched = false;
}
for (bc.isolated_worlds.items) |isolated_world| {
@@ -1631,33 +1642,40 @@ test "cdp.frame: first navigation replaces the bootstrap about:blank global" {
var ctx = try testing.context();
defer ctx.deinit();
- var bc = try ctx.loadBrowserContext(.{ .id = "BID-GLB", .target_id = "TID-GLB-000000".* });
- bc.session_id = "SID-GLB";
-
- // Create a fresh target whose root frame is the bootstrap about:blank
- // document (this is what Target.createTarget {url: "about:blank"} does).
- _ = try bc.session.createPage();
+ // A fresh, attached target sitting on its bootstrap about:blank document.
+ try ctx.processMessage(.{ .id = 69, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } });
+ try ctx.processMessage(.{ .id = 70, .method = "Target.createTarget", .params = .{ .url = "about:blank" } });
+ const bc = &ctx.cdp().browser_context.?;
{
const frame = bc.mainFrame() orelse unreachable;
try testing.expectEqualSlices(u8, "about:blank", frame.url);
}
- // Simulate a client running JS on the blank page before navigating
- // (Runtime.evaluate "window.leak = 'set-on-about-blank'").
+ // A client runs JS on the blank page before navigating. This must go
+ // through the CDP Runtime domain: that is what marks the bootstrap main
+ // world as touched and disables the in-place navigation fast path.
+ try ctx.processMessage(.{
+ .id = 71,
+ .method = "Runtime.evaluate",
+ .params = .{ .expression = "window.leak = 'set-on-about-blank'; window.leak" },
+ .sessionId = bc.session_id.?,
+ });
+ try ctx.expectSentResult(.{ .result = .{ .type = "string", .value = "set-on-about-blank" } }, .{ .id = 71 });
{
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("window.leak = 'set-on-about-blank'; window.leak", null);
+ const v = try ls.local.exec("window.leak === 'set-on-about-blank'", null);
try testing.expect(v.toBool());
}
// First real navigation. It must NOT inherit the about:blank global.
try ctx.processMessage(.{
- .id = 71,
+ .id = 72,
.method = "Page.navigate",
.params = .{ .url = "http://127.0.0.1:9582/src/browser/tests/cdp/dom1.html" },
+ .sessionId = bc.session_id.?,
});
try testing.waitForPage(bc);
@@ -1672,6 +1690,30 @@ test "cdp.frame: first navigation replaces the bootstrap about:blank global" {
try testing.expect(v2.toBool());
}
+test "cdp.frame: first navigation of a pristine bootstrap about:blank navigates in place" {
+ // Counterpart of the #3215 test: with no client JS on the bootstrap page,
+ // the first Page.navigate keeps the in-place fast path (no pending Page).
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-PRS", .target_id = "TID-PRS-000000".* });
+ bc.session_id = "SID-PRS";
+ _ = try bc.session.createPage();
+ const before = bc.mainFrame() orelse unreachable;
+ try testing.expectEqualSlices(u8, "about:blank", before.url);
+
+ try ctx.processMessage(.{
+ .id = 73,
+ .method = "Page.navigate",
+ .params = .{ .url = "http://127.0.0.1:9582/src/browser/tests/cdp/dom1.html" },
+ });
+ try testing.waitForPage(bc);
+
+ const after = bc.mainFrame() orelse unreachable;
+ try testing.expect(before == after);
+ try testing.expect(std.mem.endsWith(u8, after.url, "/cdp/dom1.html"));
+}
+
test "cdp.frame: anchor click sends Referer matching the originating page" {
// HTML Living Standard "navigate" algorithm + Fetch §4.5 "request's referrer":
// when a navigation is initiated by a hyperlink click (or form submit, or
diff --git a/src/cdp/domains/runtime.zig b/src/cdp/domains/runtime.zig
index 5912d5371..0942594b0 100644
--- a/src/cdp/domains/runtime.zig
+++ b/src/cdp/domains/runtime.zig
@@ -41,7 +41,11 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.runIfWaitingForDebugger => return cmd.sendResult(null, .{}),
.enable => return enable(cmd),
.disable => return disable(cmd),
- else => return sendInspector(cmd, action),
+ else => {
+ const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
+ bc.main_world_touched = true;
+ return sendInspector(cmd, action);
+ },
}
}