feat: fallback to DB localStorage for idb names (#8682)

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
Shivam Mishra
2024-01-15 15:08:15 +05:30
committed by GitHub
parent e7e14f01e4
commit ea7898d113
2 changed files with 30 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ export class DataManager {
async initDb() {
if (this.db) return this.db;
const dbName = `cw-store-${this.accountId}`;
this.db = await openDB(`cw-store-${this.accountId}`, DATA_VERSION, {
upgrade(db) {
db.createObjectStore('cache-keys');
@@ -19,6 +20,13 @@ export class DataManager {
},
});
// Store the database name in LocalStorage
const dbNames = JSON.parse(localStorage.getItem('cw-idb-names') || '[]');
if (!dbNames.includes(dbName)) {
dbNames.push(dbName);
localStorage.setItem('cw-idb-names', JSON.stringify(dbNames));
}
return this.db;
}

View File

@@ -44,10 +44,29 @@ export const clearLocalStorageOnLogout = () => {
};
export const deleteIndexedDBOnLogout = async () => {
const dbs = await window.indexedDB.databases();
dbs.forEach(db => {
window.indexedDB.deleteDatabase(db.name);
let dbs = [];
try {
dbs = await window.indexedDB.databases();
dbs = dbs.map(db => db.name);
} catch (e) {
dbs = JSON.parse(localStorage.getItem('cw-idb-names') || '[]');
}
dbs.forEach(dbName => {
const deleteRequest = window.indexedDB.deleteDatabase(dbName);
deleteRequest.onerror = event => {
// eslint-disable-next-line no-console
console.error(`Error deleting database ${dbName}.`, event);
};
deleteRequest.onsuccess = () => {
// eslint-disable-next-line no-console
console.log(`Database ${dbName} deleted successfully.`);
};
});
localStorage.removeItem('cw-idb-names');
};
export const clearCookiesOnLogout = () => {