Skip to content

Commit 4fc09ec

Browse files
committed
proper handling of history opt in navigate
1 parent 67f979b commit 4fc09ec

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/browser/html/location.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub const Location = struct {
5656
break :blk try std.fmt.allocPrint(page.arena, "#{s}", .{hash});
5757
};
5858

59-
return page.navigateFromWebAPI(normalized_hash, .{ .reason = .script }, .replace);
59+
return page.navigateFromWebAPI(normalized_hash, .{ .reason = .script }, .{ .replace = null });
6060
}
6161

6262
pub fn get_protocol(self: *Location) []const u8 {
@@ -96,7 +96,7 @@ pub const Location = struct {
9696
}
9797

9898
pub fn _replace(_: *const Location, url: []const u8, page: *Page) !void {
99-
return page.navigateFromWebAPI(url, .{ .reason = .script }, .replace);
99+
return page.navigateFromWebAPI(url, .{ .reason = .script }, .{ .replace = null });
100100
}
101101

102102
pub fn _reload(_: *const Location, page: *Page) !void {

src/browser/navigation/Navigation.zig

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn replaceEntry(
197197
const entry = try arena.create(NavigationHistoryEntry);
198198
entry.* = NavigationHistoryEntry{
199199
.id = id_str,
200-
.key = id_str,
200+
.key = previous.key,
201201
.url = url,
202202
.state = state,
203203
};
@@ -260,6 +260,19 @@ pub fn navigate(
260260
try page.navigateFromWebAPI(url, .{ .reason = .navigation }, kind);
261261
}
262262
},
263+
.replace => |state| {
264+
if (is_same_document) {
265+
page.url = new_url;
266+
267+
try committed.resolve({});
268+
// todo: Fire navigate event
269+
try finished.resolve({});
270+
271+
_ = try self.replaceEntry(url, .{ .source = .navigation, .value = state }, page, true);
272+
} else {
273+
try page.navigateFromWebAPI(url, .{ .reason = .navigation }, kind);
274+
}
275+
},
263276
.traverse => |index| {
264277
self.index = index;
265278

@@ -276,7 +289,6 @@ pub fn navigate(
276289
.reload => {
277290
try page.navigateFromWebAPI(url, .{ .reason = .navigation }, kind);
278291
},
279-
else => unreachable,
280292
}
281293

282294
return .{
@@ -288,7 +300,13 @@ pub fn navigate(
288300
pub fn _navigate(self: *Navigation, _url: []const u8, _opts: ?NavigateOptions, page: *Page) !NavigationReturn {
289301
const opts = _opts orelse NavigateOptions{};
290302
const json = if (opts.state) |state| state.toJson(page.session.arena) catch return error.DataClone else null;
291-
return try self.navigate(_url, .{ .push = json }, page);
303+
304+
const kind: NavigationKind = switch (opts.history) {
305+
.replace => .{ .replace = json },
306+
.push, .auto => .{ .push = json },
307+
};
308+
309+
return try self.navigate(_url, kind, page);
292310
}
293311

294312
pub const ReloadOptions = struct {

src/browser/navigation/root.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub const NavigationType = enum {
5151

5252
pub const NavigationKind = union(NavigationType) {
5353
push: ?[]const u8,
54-
replace,
54+
replace: ?[]const u8,
5555
traverse: usize,
5656
reload,
5757
};

0 commit comments

Comments
 (0)