Skip to content

Commit

Permalink
fix: improve persistence handling with global availability flag
Browse files Browse the repository at this point in the history
  • Loading branch information
vgcman16 committed Oct 30, 2024
1 parent f8a1707 commit 5f2cc9a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
12 changes: 11 additions & 1 deletion app/entry.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ async function initIndexedDB() {
}

// Initialize IndexedDB before hydrating the app
initIndexedDB().then(() => {
initIndexedDB().then((success) => {
// Set a global flag for persistence status
window.__BOLT_PERSISTENCE_AVAILABLE__ = success;

startTransition(() => {
hydrateRoot(document.getElementById('root')!, <RemixBrowser />);
});
});

// Add type declaration
declare global {
interface Window {
__BOLT_PERSISTENCE_AVAILABLE__: boolean;
}
}
45 changes: 34 additions & 11 deletions app/lib/persistence/useChatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ async function initializeDb() {
return undefined;
}

// Check if IndexedDB is available
if (!window.indexedDB) {
logger.debug('IndexedDB not available');
// Check if persistence is available
if (!window.__BOLT_PERSISTENCE_AVAILABLE__) {
logger.debug('Persistence not available');
return undefined;
}

Expand Down Expand Up @@ -68,23 +68,39 @@ export function useChatHistory() {
useEffect(() => {
const init = async () => {
try {
// Always try to initialize the database
const database = await initializeDb();

// If we have a mixedId but no database, navigate home silently
if (mixedId && !database) {
navigate('/', { replace: true });
setReady(true);
return;
}

// If we have both mixedId and database, try to load messages
if (mixedId && database) {
const storedMessages = await getMessages(database, mixedId);
if (storedMessages && storedMessages.messages.length > 0) {
setInitialMessages(storedMessages.messages);
setUrlId(storedMessages.urlId);
description.set(storedMessages.description);
chatId.set(storedMessages.id);
} else {
try {
const storedMessages = await getMessages(database, mixedId);
if (storedMessages && storedMessages.messages.length > 0) {
setInitialMessages(storedMessages.messages);
setUrlId(storedMessages.urlId);
description.set(storedMessages.description);
chatId.set(storedMessages.id);
} else {
navigate('/', { replace: true });
}
} catch (error) {
logger.error('Failed to load messages:', error);
navigate('/', { replace: true });
}
}

setReady(true);
} catch (error) {
logger.error('Failed to initialize:', error);
setReady(true);
}
setReady(true);
};

init();
Expand Down Expand Up @@ -133,3 +149,10 @@ function navigateChat(nextId: string) {
url.pathname = `/chat/${nextId}`;
window.history.replaceState({}, '', url);
}

// Add type declaration
declare global {
interface Window {
__BOLT_PERSISTENCE_AVAILABLE__: boolean;
}
}

0 comments on commit 5f2cc9a

Please sign in to comment.