-
Notifications
You must be signed in to change notification settings - Fork 3
feat: cache notifications in IndexedDB via storageManager #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,8 @@ const { CategoryID } = defineProps<{ | |
| CategoryID: number | ||
| }>() | ||
|
|
||
| const notificationCacheKey = `notifications:${CategoryID}:${locale.value}` | ||
|
|
||
| function fillInTemplate(data: string | null, message: Message) { | ||
| const re = (data ?? '') | ||
| .replace( | ||
|
|
@@ -209,15 +211,27 @@ const handleLoad = async (noTemplates = true) => { | |
| } | ||
| }) | ||
|
|
||
| items.value = [...items.value, ...defaultItems] | ||
| const merged = [...items.value, ...defaultItems] | ||
| const deduplicated = Array.from(new Map(merged.map((item) => [item.ID, item])).values()) | ||
| items.value = deduplicated | ||
| await storageManager.setObjToIDB(notificationCacheKey, items.value) | ||
| loading.value = false | ||
| skip.value += 20 | ||
| } catch (error) { | ||
| showMessage('error', String(error), { duration: 5000 }) | ||
| } | ||
| } | ||
|
|
||
| handleLoad(false) | ||
| async function restoreNotificationsFromCache() { | ||
| const cached = await storageManager.getObjFromIDB<NotificationMessage[]>(notificationCacheKey) | ||
| if (cached.status !== 'success' || cached.value == null || cached.value.length === 0) return | ||
| items.value = cached.value | ||
| skip.value = cached.value.length | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Setting Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| restoreNotificationsFromCache().finally(() => { | ||
| handleLoad(false) | ||
| }) | ||
| </script> | ||
|
|
||
| <style scoped> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new cache key only uses
CategoryIDandlocale, so cached notifications are shared across accounts on the same browser profile. On a logout/login switch,restoreNotificationsFromCache()will hydrate another user's messages before any API response arrives, which is a privacy leak and can also leave the list offset (skip) inconsistent for the new account. Add a stable per-user identifier to the key (or clear this store on auth change) to keep notification data isolated.Useful? React with 👍 / 👎.