diff --git a/src/zen/sessionstore/ZenWindowSync.sys.mjs b/src/zen/sessionstore/ZenWindowSync.sys.mjs index d498ab005..292c3c366 100644 --- a/src/zen/sessionstore/ZenWindowSync.sys.mjs +++ b/src/zen/sessionstore/ZenWindowSync.sys.mjs @@ -658,12 +658,12 @@ class nsZenWindowSync { // we would start receiving invalid history changes from the the incorrect // browser view that was just swapped out. this.#maybeFlushTabState(aOurTab).finally(() => { - if (!tabStateEntries?.length) { + if (!tabStateEntries?.entries.length) { this.log(`Error: No tab state entries found for tab ${aOtherTab.id} during swap`); return; } lazy.TabStateCache.update(aOurTab.linkedBrowser.permanentKey, { - entries: tabStateEntries, + history: tabStateEntries, }); }); return true; @@ -853,14 +853,14 @@ class nsZenWindowSync { * Retrieves the tab state entries from the cache for a given tab. * * @param {object} aTab - The tab to retrieve the state for. - * @returns {Array} The tab state entries. + * @returns {object} The tab state entries. */ #getTabEntriesFromCache(aTab) { - if (!aTab.linkedBrowser) { - return []; + let cachedState; + if (aTab.linkedBrowser) { + cachedState = lazy.TabStateCache.get(aTab.linkedBrowser.permanentKey); } - let cachedState = lazy.TabStateCache.get(aTab.linkedBrowser.permanentKey) || {}; - return cachedState.history?.entries || []; + return cachedState?.history?.entries ? cachedState.history : { entries: [] }; } /** @@ -887,13 +887,16 @@ class nsZenWindowSync { setPinnedTabState(aTab) { return this.#maybeFlushTabState(aTab).finally(() => { this.log(`Setting pinned initial state for tab ${aTab.id}`); - const entries = this.#getTabEntriesFromCache(aTab); - let activeIndex = "index" in entries ? entries.index : entries.entries.length - 1; - activeIndex = Math.min(activeIndex, entries.entries.length - 1); + let { entries, index } = this.#getTabEntriesFromCache(aTab); + let image = aTab.getAttribute("image") || aTab.ownerGlobal.gBrowser.getIcon(aTab); + let activeIndex = typeof index === "number" ? index : entries.length; + // Tab state cache gives us the index starting from 1 instead of 0. + activeIndex--; + activeIndex = Math.min(activeIndex, entries.length - 1); activeIndex = Math.max(activeIndex, 0); const initialState = { - entry: entries.entries[activeIndex], - image: entries.image, + entry: (entries[activeIndex] || entries[0]) ?? null, + image, }; this.#runOnAllWindows(null, (win) => { const targetTab = this.getItemFromWindow(win, aTab.id);