Skip to content

Lightpanda: the headless browser designed for AI and automation

history: keep the document id on the frame, not on every entry

Navid EMAD199866146708
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index cab9a0748..344a0fbcf 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -96,6 +96,11 @@ _frame_id: u32,
// navigate.
_loader_id: u32,
+// The session-history document this frame shows: its own _loader_id, unless a
+// traversal or reload recreated an earlier document, whose id it then takes
+// over so that document's other history entries stay same-document with it.
+_history_document_id: u32,
+
page: *Page,
_session: *Session,
@@ -345,6 +350,7 @@ pub fn init(self: *Frame, frame_id: u32, page: *Page, opts: InitOpts) !void {
})).asDocument();
const arena = page.frame_arena;
+ const loader_id = session.nextLoaderId();
self.* = .{
.js = undefined,
@@ -359,7 +365,8 @@ pub fn init(self: *Frame, frame_id: u32, page: *Page, opts: InitOpts) !void {
.local_arena = local_arena.allocator(),
._frame_id = frame_id,
._session = session,
- ._loader_id = session.nextLoaderId(),
+ ._loader_id = loader_id,
+ ._history_document_id = loader_id,
._factory = factory,
._pending_loads = 1, // always 1 for the ScriptManager
._type = if (parent == null) .root else .frame,
diff --git a/src/browser/webapi/navigation/Navigation.zig b/src/browser/webapi/navigation/Navigation.zig
index 903650ac6..373d091c2 100644
--- a/src/browser/webapi/navigation/Navigation.zig
+++ b/src/browser/webapi/navigation/Navigation.zig
@@ -196,17 +196,11 @@ pub fn commitNavigation(self: *Navigation, frame: *Frame) !void {
try self.updateEntries(url, kind, frame, false);
- // A traversal or reload recreates the document of the entry it lands on.
- // Every entry that shared the old document now belongs to the new one.
+ // A traversal or reload recreates the document of the entry it lands on:
+ // the new frame takes over that document's id, so the other entries of
+ // that document stay same-document with it.
switch (kind) {
- .traverse, .reload => {
- const old_loader_id = self.getCurrentEntry()._loader_id;
- for (self._entries.items) |entry| {
- if (entry._loader_id == old_loader_id) {
- entry._loader_id = frame._loader_id;
- }
- }
- },
+ .traverse, .reload => frame._history_document_id = self.getCurrentEntry()._document_id,
.push, .replace => {},
}
@@ -266,7 +260,7 @@ pub fn pushEntry(
._key = id_str,
._url = url,
._state = state,
- ._loader_id = frame._loader_id,
+ ._document_id = frame._history_document_id,
},
});
@@ -308,7 +302,7 @@ pub fn replaceEntry(
._key = previous._key,
._url = url,
._state = state,
- ._loader_id = frame._loader_id,
+ ._document_id = frame._history_document_id,
},
});
diff --git a/src/browser/webapi/navigation/NavigationHistoryEntry.zig b/src/browser/webapi/navigation/NavigationHistoryEntry.zig
index 615dbac39..bdaab163e 100644
--- a/src/browser/webapi/navigation/NavigationHistoryEntry.zig
+++ b/src/browser/webapi/navigation/NavigationHistoryEntry.zig
@@ -33,10 +33,10 @@ _id: []const u8,
_key: []const u8,
_url: ?[:0]const u8,
_state: NavigationState,
-// The document this entry belongs to, as that document's frame loader id.
-// Entries created by pushState/replaceState or a fragment navigation share
-// their document's id; traversing between them stays in that document.
-_loader_id: u32,
+// The document this entry belongs to (Frame._history_document_id). Entries
+// created by pushState/replaceState or a fragment navigation share their
+// document's id; traversing between them stays in that document.
+_document_id: u32,
_on_dispose: ?js.Function.Global = null,
@@ -69,7 +69,7 @@ pub fn key(self: *const NavigationHistoryEntry) []const u8 {
// that the URLs match: pushState can change the path or query of an entry
// without leaving its document.
pub fn sameDocument(self: *const NavigationHistoryEntry, frame: *Frame) bool {
- return self._loader_id == frame._loader_id;
+ return self._document_id == frame._history_document_id;
}
pub fn url(self: *const NavigationHistoryEntry) ?[:0]const u8 {