Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(ui): make palette recents resilient to blocked storage

joshistoast7bfdf5b340ee
diff --git a/invokeai/frontend/webv2/src/workbench/palette/recents.test.ts b/invokeai/frontend/webv2/src/workbench/palette/recents.test.ts
index 2df304fb0d..f326294a26 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/recents.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/recents.test.ts
@@ -5,16 +5,19 @@ import { getRecentEntryIds, recordRecentEntry } from './recents';
const STORAGE_KEY = 'invokeai:v7:webv2:palette-recents';
const storage = new Map<string, string>();
-vi.stubGlobal('window', {
- localStorage: {
- getItem: (key: string): string | null => storage.get(key) ?? null,
- setItem: (key: string, value: string): void => {
- storage.set(key, value);
- },
+const localStorageStub = {
+ getItem: (key: string): string | null => storage.get(key) ?? null,
+ setItem: (key: string, value: string): void => {
+ storage.set(key, value);
},
-});
+};
+
+vi.stubGlobal('window', { localStorage: localStorageStub });
-beforeEach(() => storage.clear());
+beforeEach(() => {
+ storage.clear();
+ vi.restoreAllMocks();
+});
describe('palette recents', () => {
it('persists only explicitly durable entries', () => {
@@ -30,4 +33,23 @@ describe('palette recents', () => {
expect(getRecentEntryIds()).toEqual(['legacy.command', 'legacy.setting']);
});
+
+ it('returns no recents when storage access throws', () => {
+ vi.spyOn(localStorageStub, 'getItem').mockImplementation(() => {
+ throw new DOMException('Storage disabled', 'SecurityError');
+ });
+
+ expect(getRecentEntryIds()).toEqual([]);
+ });
+
+ it('keeps recording non-fatal when the initial read throws', () => {
+ const getItem = vi.spyOn(localStorageStub, 'getItem').mockImplementation(() => {
+ throw new DOMException('Storage disabled', 'SecurityError');
+ });
+
+ expect(() => recordRecentEntry({ id: 'app.invoke', isPersistentRecent: true })).not.toThrow();
+
+ getItem.mockRestore();
+ expect(getRecentEntryIds()).toEqual(['app.invoke']);
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/palette/recents.ts b/invokeai/frontend/webv2/src/workbench/palette/recents.ts
index 52f9dfffd8..9737f820bc 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/recents.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/recents.ts
@@ -21,8 +21,6 @@ interface RecentUse {
uses: number;
}
-const isBrowser = (): boolean => typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
-
const parseRecords = (raw: string | null): RecentUse[] => {
if (!raw) {
return [];
@@ -58,7 +56,21 @@ const parseRecords = (raw: string | null): RecentUse[] => {
}
};
-const readRecords = (): RecentUse[] => (isBrowser() ? parseRecords(window.localStorage.getItem(STORAGE_KEY)) : []);
+const readRawRecords = (): string | null => {
+ if (typeof window === 'undefined') {
+ return null;
+ }
+
+ try {
+ // Accessing localStorage itself can throw (SecurityError) when storage is
+ // disabled or blocked, not just getItem — keep both inside the guard.
+ return window.localStorage.getItem(STORAGE_KEY);
+ } catch {
+ return null;
+ }
+};
+
+const readRecords = (): RecentUse[] => parseRecords(readRawRecords());
/** Recency multiplier: recent use outweighs raw counts, but never zeroes them. */
const recencyWeight = (ageMs: number): number => {
@@ -89,7 +101,7 @@ export const getRecentEntryIds = (): string[] => {
};
export const recordRecentEntry = (entry: Pick<PaletteEntry, 'id' | 'isPersistentRecent'>): void => {
- if (!entry.isPersistentRecent || !isBrowser()) {
+ if (!entry.isPersistentRecent || typeof window === 'undefined') {
return;
}