Skip to content

Build your personal knowledge base with Trilium Notes

client: fix broken forward navigation

Adorian Doranecb20afc6bec
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 972d3c1f4d..1b1847fb95 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -815,7 +815,7 @@ function registerWindowHandlers(appContext: AppContext) {
const { notePath, ntxId, viewScope, searchString } = linkService.parseNavigationStateFromUrl(window.location.href);
if (notePath || ntxId) {
- appContext.tabManager.switchToNoteContext(ntxId, notePath, viewScope);
+ appContext.tabManager.switchToHistoryEntry(ntxId, notePath, viewScope);
} else if (searchString) {
appContext.triggerCommand("searchNotes", { searchString });
}
diff --git a/apps/client/src/components/tab_manager.spec.ts b/apps/client/src/components/tab_manager.spec.ts
index f8f5fb0626..caf579e019 100644
--- a/apps/client/src/components/tab_manager.spec.ts
+++ b/apps/client/src/components/tab_manager.spec.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { buildNote } from "../test/easy-froca.js";
import appContext from "./app_context.js";
@@ -311,3 +311,72 @@ describe("note contexts outside the tab row", () => {
expect(tm.getCommandContext("_nowhere")).toBeNull();
});
});
+
+describe("going to an entry of the navigation history", () => {
+ let tm: TabManager;
+
+ beforeEach(() => {
+ // `appContext.triggerEvent` passes on the note switch, and an earlier test mocks it.
+ vi.restoreAllMocks();
+ buildNote({
+ id: "root",
+ title: "root",
+ children: [
+ { id: "historyA", title: "A" },
+ { id: "historyB", title: "B" },
+ { id: "historyC", title: "C" }
+ ]
+ });
+ tm = new TabManager();
+ // A note context reports a note switch through the app, which passes it to its children.
+ appContext.tabManager = tm;
+ appContext.child(tm);
+ });
+
+ afterEach(() => {
+ appContext.removeChild(tm);
+ vi.restoreAllMocks();
+ window.history.replaceState(null, "", "/");
+ });
+
+ it("rewrites the entry of another tab instead of pushing over the entries ahead", async () => {
+ const first = await tm.openEmptyTab();
+ const second = await tm.openEmptyTab();
+ await tm.activateNoteContext(first.ntxId);
+ await first.setNote("root/historyA");
+ await first.setNote("root/historyB");
+ await tm.activateNoteContext(second.ntxId);
+ await second.setNote("root/historyC");
+
+ // The browser is back on the entry of A, which the first tab has since left for B.
+ const entry = `#root/historyA?ntxId=${first.ntxId}`;
+ window.history.replaceState(null, "", entry);
+ const pushState = vi.spyOn(window.history, "pushState");
+
+ await tm.switchToHistoryEntry(first.ntxId, "root/historyA", { viewMode: "default" });
+ expect(pushState).not.toHaveBeenCalled();
+ expect(tm.activeNtxId).toBe(first.ntxId);
+ expect(first.notePath).toBe("root/historyA");
+ expect(window.location.hash).toBe(entry);
+
+ // A note opened afterwards is a new entry again.
+ await first.setNote("root/historyC");
+ expect(pushState).toHaveBeenCalledOnce();
+ expect(window.location.hash).toBe(`#root/historyC?ntxId=${first.ntxId}`);
+ });
+
+ it("rewrites the entry of a closed tab with the tab that reopens it", async () => {
+ const tab = await tm.openEmptyTab();
+ await tm.activateNoteContext(tab.ntxId);
+ window.history.replaceState(null, "", "#root/historyA?ntxId=closedTab");
+ const pushState = vi.spyOn(window.history, "pushState");
+
+ await tm.switchToHistoryEntry("closedTab", "root/historyA", { viewMode: "default" });
+
+ expect(pushState).not.toHaveBeenCalled();
+ const reopened = tm.getActiveContext();
+ expect(reopened).not.toBe(tab);
+ expect(reopened?.notePath).toBe("root/historyA");
+ expect(window.location.hash).toBe(`#root/historyA?ntxId=${reopened?.ntxId}`);
+ });
+});
diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.ts
index 6860be6a50..41af25e9e3 100644
--- a/apps/client/src/components/tab_manager.ts
+++ b/apps/client/src/components/tab_manager.ts
@@ -37,6 +37,8 @@ export default class TabManager extends Component {
public activeNtxId: string | null;
public recentlyClosedTabs: TabState[];
public tabsUpdate: SpacedUpdate;
+ /** Set while `switchToHistoryEntry()` runs: the history entry is replaced instead of pushed. */
+ private isShowingHistoryEntry = false;
constructor() {
super();
@@ -185,7 +187,11 @@ export default class TabManager extends Component {
// update if it's the first history entry or there has been a change
if (window.history.length === 0 || calculatedHash !== window.location?.hash) {
// using pushState instead of directly modifying document.location because it does not trigger hashchange
- window.history.pushState(null, "", calculatedHash);
+ if (this.isShowingHistoryEntry) {
+ window.history.replaceState(null, "", calculatedHash);
+ } else {
+ window.history.pushState(null, "", calculatedHash);
+ }
}
const activeNoteContext = this.getActiveContext();
@@ -289,6 +295,23 @@ export default class TabManager extends Component {
}
}
+ /**
+ * Shows the history entry the browser went to. The state this reaches replaces the entry, so
+ * the entries after it stay reachable.
+ */
+ async switchToHistoryEntry(
+ ntxId: string | null,
+ notePath: string,
+ viewScope: Record<string, any> = {}
+ ) {
+ this.isShowingHistoryEntry = true;
+ try {
+ await this.switchToNoteContext(ntxId, notePath, viewScope);
+ } finally {
+ this.isShowingHistoryEntry = false;
+ }
+ }
+
async openAndActivateEmptyTab() {
const noteContext = await this.openEmptyTab();
await this.activateNoteContext(noteContext.ntxId);