diff --git a/app/entry.client.tsx b/app/entry.client.tsx index ef8f29056..36a3d6023 100644 --- a/app/entry.client.tsx +++ b/app/entry.client.tsx @@ -21,37 +21,69 @@ async function initIndexedDB() { try { // For Chrome, we need to be more careful with initialization if (isChrome()) { - // First, try to delete any existing database to ensure a clean state - const deleteRequest = window.indexedDB.deleteDatabase('boltHistory'); - deleteRequest.onsuccess = () => { - // Now create a fresh database - const request = window.indexedDB.open('boltHistory', 1); - request.onerror = () => { - logger.error('Failed to open database'); - window.__BOLT_PERSISTENCE_AVAILABLE__ = false; - resolve(false); - }; + // First, try to open a test database + const testRequest = window.indexedDB.open('test', 1); + testRequest.onerror = () => { + logger.error('Test database failed'); + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + resolve(false); + }; + + testRequest.onsuccess = () => { + // Close and delete test database + const testDb = testRequest.result; + testDb.close(); + const deleteRequest = window.indexedDB.deleteDatabase('test'); + + deleteRequest.onsuccess = () => { + // Now try to open the actual database + const request = window.indexedDB.open('boltHistory', 1); + + request.onerror = () => { + logger.error('Failed to open database'); + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + resolve(false); + }; - request.onupgradeneeded = (event) => { - const db = (event.target as IDBOpenDBRequest).result; - if (!db.objectStoreNames.contains('chats')) { - const store = db.createObjectStore('chats', { keyPath: 'id' }); - store.createIndex('id', 'id', { unique: true }); - store.createIndex('urlId', 'urlId', { unique: true }); - } + request.onupgradeneeded = (event) => { + const db = (event.target as IDBOpenDBRequest).result; + if (!db.objectStoreNames.contains('chats')) { + const store = db.createObjectStore('chats', { keyPath: 'id' }); + store.createIndex('id', 'id', { unique: true }); + store.createIndex('urlId', 'urlId', { unique: true }); + } + }; + + request.onsuccess = (event) => { + const db = (event.target as IDBOpenDBRequest).result; + + // Test if we can actually use the database + try { + const transaction = db.transaction(['chats'], 'readonly'); + transaction.oncomplete = () => { + logger.debug('Database test successful'); + window.__BOLT_PERSISTENCE_AVAILABLE__ = true; + resolve(true); + }; + transaction.onerror = () => { + logger.error('Database test failed'); + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + resolve(false); + }; + } catch (error) { + logger.error('Error testing database:', error); + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + resolve(false); + } + }; }; - request.onsuccess = () => { - logger.debug('Database initialized'); - window.__BOLT_PERSISTENCE_AVAILABLE__ = true; - resolve(true); + deleteRequest.onerror = () => { + logger.error('Failed to delete test database'); + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + resolve(false); }; }; - deleteRequest.onerror = () => { - logger.error('Failed to delete old database'); - window.__BOLT_PERSISTENCE_AVAILABLE__ = false; - resolve(false); - }; } else { // For other browsers, use the standard approach const request = window.indexedDB.open('boltHistory', 1); diff --git a/app/root.tsx b/app/root.tsx index 6a57f64d2..c653d358c 100644 --- a/app/root.tsx +++ b/app/root.tsx @@ -60,27 +60,56 @@ const inlineThemeCode = stripIndents` if (isChrome) { // For Chrome, we need to be more careful with initialization - const deleteRequest = window.indexedDB.deleteDatabase('boltHistory'); - deleteRequest.onsuccess = () => { - const request = window.indexedDB.open('boltHistory', 1); - request.onupgradeneeded = (event) => { - const db = event.target.result; - if (!db.objectStoreNames.contains('chats')) { - const store = db.createObjectStore('chats', { keyPath: 'id' }); - store.createIndex('id', 'id', { unique: true }); - store.createIndex('urlId', 'urlId', { unique: true }); - } - }; - request.onsuccess = () => { - window.__BOLT_PERSISTENCE_AVAILABLE__ = true; + const testRequest = window.indexedDB.open('test', 1); + testRequest.onerror = () => { + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + }; + + testRequest.onsuccess = () => { + // Close and delete test database + const testDb = testRequest.result; + testDb.close(); + const deleteRequest = window.indexedDB.deleteDatabase('test'); + + deleteRequest.onsuccess = () => { + // Now try to open the actual database + const request = window.indexedDB.open('boltHistory', 1); + + request.onupgradeneeded = (event) => { + const db = event.target.result; + if (!db.objectStoreNames.contains('chats')) { + const store = db.createObjectStore('chats', { keyPath: 'id' }); + store.createIndex('id', 'id', { unique: true }); + store.createIndex('urlId', 'urlId', { unique: true }); + } + }; + + request.onsuccess = (event) => { + const db = event.target.result; + + // Test if we can actually use the database + try { + const transaction = db.transaction(['chats'], 'readonly'); + transaction.oncomplete = () => { + window.__BOLT_PERSISTENCE_AVAILABLE__ = true; + }; + transaction.onerror = () => { + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + }; + } catch (error) { + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + } + }; + + request.onerror = () => { + window.__BOLT_PERSISTENCE_AVAILABLE__ = false; + }; }; - request.onerror = () => { + + deleteRequest.onerror = () => { window.__BOLT_PERSISTENCE_AVAILABLE__ = false; }; }; - deleteRequest.onerror = () => { - window.__BOLT_PERSISTENCE_AVAILABLE__ = false; - }; } else { // For other browsers, use the standard approach const request = window.indexedDB.open('boltHistory', 1);