Skip to content

Commit c009669

Browse files
committed
properly handle replace navigation case
1 parent 0e3f183 commit c009669

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/browser/navigation/Navigation.zig

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,27 @@ pub fn _forward(self: *Navigation, page: *Page) !NavigationReturn {
101101
return self.navigate(next_entry.url, .{ .traverse = new_index }, page);
102102
}
103103

104+
pub fn updateEntries(self: *Navigation, url: []const u8, kind: NavigationKind, page: *Page, dispatch: bool) !void {
105+
switch (kind) {
106+
.replace => {
107+
_ = try self.replaceEntry(url, null, page, dispatch);
108+
},
109+
.push => |state| {
110+
_ = try self.pushEntry(url, state, page, dispatch);
111+
},
112+
.traverse => |index| {
113+
self.index = index;
114+
},
115+
.reload => {},
116+
}
117+
}
118+
104119
// This is for after true navigation processing, where we need to ensure that our entries are up to date.
105120
// This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct.
106121
pub fn processNavigation(self: *Navigation, page: *Page) !void {
107122
const url = page.url.raw;
108-
const kind = page.session.navigation_kind;
109-
110-
if (kind) |k| {
111-
switch (k) {
112-
.replace => {
113-
// When replacing, we just update the URL but the state is nullified.
114-
const entry = self.currentEntry();
115-
entry.url = url;
116-
entry.state = null;
117-
},
118-
.push => |state| {
119-
_ = try self.pushEntry(url, state, page, false);
120-
},
121-
.traverse, .reload => {},
122-
}
123-
} else {
124-
_ = try self.pushEntry(url, null, page, false);
125-
}
123+
const kind: NavigationKind = page.session.navigation_kind orelse .{ .push = null };
124+
try self.updateEntries(url, kind, page, false);
126125
}
127126

128127
/// Pushes an entry into the Navigation stack WITHOUT actually navigating to it.
@@ -166,6 +165,33 @@ pub fn pushEntry(self: *Navigation, _url: []const u8, state: ?[]const u8, page:
166165
return entry;
167166
}
168167

168+
pub fn replaceEntry(self: *Navigation, _url: []const u8, state: ?[]const u8, page: *Page, dispatch: bool) !*NavigationHistoryEntry {
169+
const arena = page.session.arena;
170+
const url = try arena.dupe(u8, _url);
171+
172+
const previous = self.currentEntry();
173+
174+
const id = self.next_entry_id;
175+
self.next_entry_id += 1;
176+
const id_str = try std.fmt.allocPrint(arena, "{d}", .{id});
177+
178+
const entry = try arena.create(NavigationHistoryEntry);
179+
entry.* = NavigationHistoryEntry{
180+
.id = id_str,
181+
.key = id_str,
182+
.url = url,
183+
.state = state,
184+
};
185+
186+
self.entries.items[self.index] = entry;
187+
188+
if (dispatch) {
189+
NavigationCurrentEntryChangeEvent.dispatch(self, previous, .replace);
190+
}
191+
192+
return entry;
193+
}
194+
169195
const NavigateOptions = struct {
170196
const NavigateOptionsHistory = enum {
171197
pub const ENUM_JS_USE_TAG = true;

src/browser/page.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,7 @@ pub const Page = struct {
10751075

10761076
if (try self.url.eqlDocument(&new_url, session.transfer_arena)) {
10771077
self.url = new_url;
1078-
1079-
const prev = session.navigation.currentEntry();
1080-
NavigationCurrentEntryChangeEvent.dispatch(&self.session.navigation, prev, kind);
1078+
try session.navigation.updateEntries(stitched_url, kind, self, true);
10811079
return;
10821080
}
10831081
}

0 commit comments

Comments
 (0)