Skip to content

Commit

Permalink
fix: improve Chrome database initialization with test database
Browse files Browse the repository at this point in the history
  • Loading branch information
vgcman16 committed Oct 30, 2024
1 parent 5b4025f commit cfe96c2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 43 deletions.
84 changes: 58 additions & 26 deletions app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 46 additions & 17 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cfe96c2

Please sign in to comment.