diff --git a/src/common/providers/ProviderBootstrapLogic.tsx b/src/common/providers/ProviderBootstrapLogic.tsx index 848440c33..7e4a6203d 100644 --- a/src/common/providers/ProviderBootstrapLogic.tsx +++ b/src/common/providers/ProviderBootstrapLogic.tsx @@ -6,6 +6,7 @@ import { markNewsAsSeen, shallRedirectToNews } from '../../apps/news/news.versio import { autoConfInitiateConfiguration } from '~/common/logic/autoconf'; import { navigateToNews, ROUTE_APP_CHAT } from '~/common/app.routes'; +import { requestPersistentStorage } from '~/common/util/storageUtils'; import { useNextLoadProgress } from '~/common/components/useNextLoadProgress'; @@ -30,10 +31,13 @@ export function ProviderBootstrapLogic(props: { children: React.ReactNode }) { // [gc] garbage collection(s) React.useEffect(() => { - // Remove old attachment drafts (not persisted in chats) - // void gcAttachmentDBlobs(); // fire/forget - // Remove chat dblobs (not persisted in chat fragments) - void gcChatImageAssets(); // fire/forget + // Request persistent storage for the current origin, so that indexedDB's content is not evicted. + requestPersistentStorage().finally(() => { + // GC: Remove chat dblobs (not persisted in chat fragments) + void gcChatImageAssets(); // fire/forget + // GC: Remove old attachment drafts (not persisted in chats) + // void gcAttachmentDBlobs(); // fire/forget + }); }, []); diff --git a/src/common/util/storageUtils.ts b/src/common/util/storageUtils.ts new file mode 100644 index 000000000..20720e16c --- /dev/null +++ b/src/common/util/storageUtils.ts @@ -0,0 +1,26 @@ +import { isBrowser } from '~/common/util/pwaUtils'; + +// enable debugging of the persistent storage +const DEBUG_PERSISTENCE = false; + +/** + * Request persistent storage for the current origin, so that indexedDB's content is not evicted. + */ +export async function requestPersistentStorage(): Promise { + if (isBrowser && navigator.storage && navigator.storage.persist) { + const isPersisted = await navigator.storage.persisted(); + if (isPersisted) { + if (DEBUG_PERSISTENCE) + console.log('Storage is already persisted'); + return true; + } + + const isGranted = await navigator.storage.persist(); + if (DEBUG_PERSISTENCE) + console.log(`Persistent storage granted: ${isGranted}`); + return isGranted; + } + + console.warn('Persistent storage is not supported by this browser'); + return false; +} diff --git a/src/modules/dblobs/dblobs.db.ts b/src/modules/dblobs/dblobs.db.ts index 158606c28..abb2c61e6 100644 --- a/src/modules/dblobs/dblobs.db.ts +++ b/src/modules/dblobs/dblobs.db.ts @@ -9,6 +9,10 @@ import { DBlobAsset, DBlobAssetId, DBlobAssetType, DBlobDBAsset } from './dblobs * * [DEV NOTE] To delete the full DB (don't do it!): * - indexedDB.deleteDatabase('NAME').onsuccess = console.log; + * + * [DEV NOTE] This is related to storageUtils.ts's requestPersistentStorage, + * aswe need to request persistent storage for the current origin, sot that + * indexedDB's content is not evicted. */ class BigAgiDB extends Dexie { largeAssets!: Dexie.Table; @@ -23,9 +27,7 @@ class BigAgiDB extends Dexie { } } -/** - * In development mode, reuse the same instance of the DB to avoid re-creating it on every hot reload - */ +// In development mode, reuse the same instance of the DB to avoid re-creating it on every hot reload const globalForDexie = globalThis as unknown as { bigAgiDB: BigAgiDB | undefined; };