diff --git a/src/browser/html/Navigation.zig b/src/browser/html/Navigation.zig index c7eb9276..1394bbd5 100644 --- a/src/browser/html/Navigation.zig +++ b/src/browser/html/Navigation.zig @@ -38,8 +38,14 @@ pub const Interfaces = .{ NavigationHistoryEntry, }; -pub const NavigationKind = union(enum) { - initial, +pub const NavigationType = enum { + push, + replace, + traverse, + reload, +}; + +pub const NavigationKind = union(NavigationType) { push: ?[]const u8, replace, traverse: usize, @@ -191,21 +197,26 @@ pub fn _forward(self: *Navigation, page: *Page) !NavigationReturn { } // This is for after true navigation processing, where we need to ensure that our entries are up to date. -pub fn processNavigation(self: *Navigation, url: []const u8, kind: NavigationKind, page: *Page) !void { - switch (kind) { - .initial => { - _ = try self.pushEntry(url, null, page); - }, - .replace => { - // When replacing, we just update the URL but the state is nullified. - const entry = self.currentEntry(); - entry.url = url; - entry.state = null; - }, - .push => |state| { - _ = try self.pushEntry(url, state, page); - }, - .traverse, .reload => {}, +// This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct. +pub fn processNavigation(self: *Navigation, page: *Page) !void { + const url = page.url.raw; + const kind = page.session.navigation_kind; + + if (kind) |k| { + switch (k) { + .replace => { + // When replacing, we just update the URL but the state is nullified. + const entry = self.currentEntry(); + entry.url = url; + entry.state = null; + }, + .push => |state| { + _ = try self.pushEntry(url, state, page); + }, + .traverse, .reload => {}, + } + } else { + _ = try self.pushEntry(url, null, page); } } diff --git a/src/browser/page.zig b/src/browser/page.zig index 15caacad..631283ef 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -817,7 +817,7 @@ pub const Page = struct { } // We need to handle different navigation types differently. - try self.session.navigation.processNavigation(self.url.raw, self.session.navigation_kind, self); + try self.session.navigation.processNavigation(self); } fn pageErrorCallback(ctx: *anyopaque, err: anyerror) void { diff --git a/src/browser/session.zig b/src/browser/session.zig index d491cf27..76fdcad6 100644 --- a/src/browser/session.zig +++ b/src/browser/session.zig @@ -60,7 +60,7 @@ pub const Session = struct { // https://developer.mozilla.org/en-US/docs/Web/API/History history: History = .{}, navigation: Navigation = .{}, - navigation_kind: NavigationKind = .initial, + navigation_kind: ?NavigationKind = null, page: ?Page = null,