Skip to content

Commit ad8d575

Browse files
committed
fix: gracefully handle localStorage quota exceeding
- wrap dangerous function call with try-catch - if it is an expected error, disable caching for conversations, no longer try to save in this session - impact is minimal, should affect only users with 1000+ rooms, but other Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent e1aca90 commit ad8d575

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

src/store/conversationsStore.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ const DUMMY_CONVERSATION = {
108108
isDummyConversation: true,
109109
}
110110

111+
/** FIXME use IndexedDB for storing data (with proper offline-support)
112+
* localStorage has a limit of information it can store per browser page.
113+
* If exceeded (1000+ conversations for a user), do not cache conversations list
114+
*/
115+
let isLocalStorageQuotaExceeded = false
116+
111117
/**
112118
* Emit global event for user status update with the status from a 1-1 conversation
113119
*
@@ -486,14 +492,30 @@ const actions = {
486492
* @param {object} context default store context
487493
*/
488494
cacheConversations(context) {
495+
if (isLocalStorageQuotaExceeded) {
496+
// Ignore caching to BrowserStorage
497+
return
498+
}
499+
489500
const conversations = context.getters.conversationsList
490501
if (!conversations.length) {
491502
return
492503
}
493504

494-
const serializedConversations = JSON.stringify(conversations)
495-
BrowserStorage.setItem('cachedConversations', serializedConversations)
496-
console.debug(`Conversations were saved to BrowserStorage. Estimated object size: ${(serializedConversations.length / 1024).toFixed(2)} kB`)
505+
try {
506+
const serializedConversations = JSON.stringify(conversations)
507+
BrowserStorage.setItem('cachedConversations', serializedConversations)
508+
console.debug(`Conversations were saved to BrowserStorage. Estimated object size: ${(serializedConversations.length / 1024).toFixed(2)} kB`)
509+
}
510+
catch (error) {
511+
if (error.name === 'QuotaExceededError') {
512+
console.error('Too many conversations to cache, disabling: ', error)
513+
isLocalStorageQuotaExceeded = true
514+
BrowserStorage.removeItem('cachedConversations')
515+
} else {
516+
console.error('Error while caching conversations: ', error)
517+
}
518+
}
497519
},
498520

499521
/**

0 commit comments

Comments
 (0)