Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: restore a same-document entry in place in Page.navigateToHistoryEntry

Navid EMADc430a8982836
diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zig
index f30d2884a..34980b3e1 100644
--- a/src/browser/webapi/History.zig
+++ b/src/browser/webapi/History.zig
@@ -137,7 +137,14 @@ fn goInner(delta: i32, frame: *Frame) !void {
return;
}
- const index = @as(usize, @intCast(index_s));
+ try traverse(@intCast(index_s), frame);
+}
+
+// Traverses the session history to the entry at `index`. An entry of this
+// document is restored in place, firing popstate; any other loads its
+// document. Needs an entered JS context (navigateInner creates promises).
+pub fn traverse(index: usize, frame: *Frame) !void {
+ const navigation = frame._session.navigationFor(frame);
const entry = navigation._entries.items[index];
// popstate fires on the document that stays; a traversal to another
// document replaces this one instead.
diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zig
index 621a39bf8..a45683d97 100644
--- a/src/server/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -28,6 +28,7 @@ const network = @import("network.zig");
const js = @import("../../../browser/js/js.zig");
const URL = @import("../../../browser/URL.zig");
const Frame = @import("../../../browser/Frame.zig");
+const History = @import("../../../browser/webapi/History.zig");
const referrer = @import("../../../browser/referrer.zig");
const Notification = @import("../../../Notification.zig");
@@ -503,6 +504,21 @@ fn navigateToHistoryEntry(cmd: *CDP.Command) !void {
const idx = target_index orelse return error.InvalidParams;
const url = target_url orelse return error.InvalidParams;
+ // An entry of the current document (pushState, fragment) is restored in
+ // place, as history.back() does: same document, popstate, no reload.
+ if (nav._entries.items[idx].sameDocument(frame)) {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+
+ var caller: js.Caller = undefined;
+ caller.initWithContext(frame.js, ls.local.handle);
+ defer caller.deinit();
+
+ try History.traverse(idx, frame);
+ return cmd.sendResult(null, .{});
+ }
+
const opts = Frame.NavigateOpts{
.reason = .history,
.cdp_id = cmd.input.id,
@@ -2910,6 +2926,45 @@ test "cdp.frame: addScriptToEvaluateOnNewDocument" {
}
}
+test "cdp.frame: navigateToHistoryEntry restores an entry of the same document in place" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ var bc = try ctx.loadBrowserContext(.{ .id = "BID-B3", .url = "hi.html", .target_id = "TID-B3-0000000".* });
+ const frame = bc.mainFrame() orelse unreachable;
+
+ {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec(
+ \\window.__marker = 'kept';
+ \\window.__pops = [];
+ \\addEventListener('popstate', (e) => __pops.push(location.pathname + ' ' + JSON.stringify(e.state)));
+ \\history.pushState({n: 1}, '', '/next');
+ , null);
+ }
+
+ // Entry 1 is hi.html, entry 2 the pushState one. Going back to entry 1
+ // must not reload the document: like history.back(), it restores the
+ // entry in place and fires popstate, then answers at once.
+ try ctx.processMessage(.{
+ .id = 50,
+ .method = "Page.navigateToHistoryEntry",
+ .params = .{ .entryId = 1 },
+ });
+ try ctx.expectSentResult(null, .{ .id = 50 });
+
+ try testing.expectEqualSlices(u8, "http://127.0.0.1:9582/src/browser/tests/hi.html", frame.url);
+ {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const v = try ls.local.exec("window.__marker + '|' + __pops.join(',')", null);
+ try testing.expectEqualSlices(u8, "kept|/src/browser/tests/hi.html null", try v.toStringSlice());
+ }
+}
+
test "cdp.frame: getNavigationHistory + navigateToHistoryEntry" {
var ctx = try testing.context();
defer ctx.deinit();