Skip to content

Switch from localstorage to IndexedDB for unbounded storage#18

Merged
dsfaccini merged 2 commits intopydantic:mainfrom
prescod:switch-from-localstorage-to-indexeddb
Apr 6, 2026
Merged

Switch from localstorage to IndexedDB for unbounded storage#18
dsfaccini merged 2 commits intopydantic:mainfrom
prescod:switch-from-localstorage-to-indexeddb

Conversation

@prescod
Copy link
Copy Markdown
Contributor

@prescod prescod commented Apr 1, 2026

IndexedDB is unlimited.

Maybe I should also implement warnings for when the chat crosses certain thresholds so you remember to delete your chats and not waste disk space? Let me know if you want that as a fast-follow PR.

But the old way was a hard limit that would crash a demo in the middle.

Fixes #17

prescod and others added 2 commits April 1, 2026 05:32
- Replace window.alert() with toast notifications (sonner) for storage errors
- Change useLayoutEffect to useEffect for async message loading
- Remove `as` typecasts from chat-db, use typed variables with eslint-disable
  at IDB/JSON.parse boundaries instead
- Add db.onclose handler to reset cached dbPromise on connection loss
- Defer localStorage cleanup in migration until all IDB writes succeed
- Render app shell during migration instead of blank screen
- Type messages as UIMessage[] end-to-end through chat-db

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dsfaccini
Copy link
Copy Markdown
Contributor

dsfaccini commented Apr 4, 2026

hey @prescod , appreciate the contrib! I had claude do a review and we found some points for potential improvement, so I'll push an update and merge later. thanks again!

@dsfaccini
Copy link
Copy Markdown
Contributor

Claude here: Pushed a follow-up commit with some improvements.

Changes

src/lib/chat-db.ts:

  • Replaced window.alert() with toast.error() (sonner) for storage errors
  • Removed as typecasts — used typed variable declarations with targeted eslint-disable at IDB/JSON.parse boundaries
  • Added db.onclose handler to reset cached dbPromise on connection loss
  • Reset dbPromise on open error to avoid reusing a rejected promise
  • Deferred localStorage cleanup in migration until all IDB writes succeed
  • Typed messages as UIMessage[] end-to-end instead of unknown[]

src/Chat.tsx:

  • Changed useLayoutEffectuseEffect for async message loading (layout effect had no benefit with async IDB)
  • Removed as typeof messages cast (no longer needed with typed getMessages)
  • Removed unused useLayoutEffect import

src/App.tsx:

  • Render app shell during migration instead of blank screen — only Chat is gated on migration completion

@dsfaccini dsfaccini merged commit b4d1010 into pydantic:main Apr 6, 2026
1 check passed
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 7, 2026

🎉 This PR is included in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@prescod prescod deleted the switch-from-localstorage-to-indexeddb branch April 10, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LocalStorage has a hard limit of 5MB of space

2 participants