Skip to content

Conversation

@typelets
Copy link
Owner

Problem

/api/notes/counts returned stale cached data after creating, updating, or deleting notes/folders. Mobile app displayed incorrect counts, especially for nested subfolders.

Solution

Added recursive cache invalidation that clears counts for all ancestor folders when notes or folders are mutated.

Changes

src/lib/cache.ts

  • Added getAncestorFolderIds() - Recursively get all ancestor folder IDs
  • Added invalidateNoteCounts() - Invalidate counts for user and all ancestors
  • Added invalidateNoteCountsForMove() - Handle cache when items move between folders

src/routes/notes/crud.ts

  • Added cache invalidation to POST /api/notes (create)
  • Added cache invalidation to PUT /api/notes/:id (update)
  • Added cache invalidation to DELETE /api/notes/:id (delete)

src/routes/notes/actions.ts

  • Added cache invalidation to POST /api/notes/:id/star
  • Added cache invalidation to POST /api/notes/:id/restore
  • Added cache invalidation to POST /api/notes/:id/hide
  • Added cache invalidation to POST /api/notes/:id/unhide

src/routes/folders/crud.ts

  • Added cache invalidation to POST /api/folders (create)
  • Added cache invalidation to PUT /api/folders/:id (update)
  • Added cache invalidation to DELETE /api/folders/:id (delete)

How It Works

On any mutation:

  1. Invalidate global user counts: notes:${userId}:counts
  2. Invalidate immediate folder: notes:${userId}:folder:${folderId}:counts
  3. Recursively invalidate all ancestor folders

Testing

  • ✅ Build successful (164.5kb)
  • ✅ TypeScript compilation passes

Fixes stale cache in /api/notes/counts endpoint for mobile app.

@typelets typelets self-assigned this Oct 17, 2025
@typelets typelets merged commit 93a81a2 into main Oct 17, 2025
4 checks passed
@typelets
Copy link
Owner Author

🎉 This PR is included in version 1.8.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

3 participants