fix(@novu/js): dedupe notifications in cache.unshift by id#10797
fix(@novu/js): dedupe notifications in cache.unshift by id#10797NianJiuZst wants to merge 2 commits intonovuhq:mainfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hey there and thank you for opening this pull request! 👋 We require pull request titles to follow specific formatting rules and it looks like your proposed title needs to be adjusted. Your PR title is: Requirements:
Expected format: Details: |
Summary
Fixes #10762.
When
notifications.cache.unshift()is called with a notification that has the sameidas an existing notification in the cache, it now skips the insert to avoid duplicates. This prevents the same notification from appearing multiple times inuseNotifications()when multiple Novu client instances (e.g., multiple browser tabs) receive the same socket event.Root Cause
The
unshiftmethod inpackages/js/src/cache/notifications-cache.tswas unconditionally prepending the notification to the cache without checking if a notification with the sameidalready exists. When multiple Novu clients receive the same WebSocket notification event, each one callsunshiftwith the same notification data, resulting in N duplicates for N active clients.Fix
Added a guard in
unshift()that checks if a notification with the sameidalready exists in the cache before prepending:Testing
Added two new test cases:
should not add duplicate notification when unshift is called with the same id- verifies deduplicationshould allow unshift of notification with different id- verifies normal behavior still worksReproduction (from issue)
Note
Low Risk
Low risk: small behavioral guard in
NotificationsCache.unshiftplus new unit tests; main impact is suppressing inserts when anidalready exists, which could affect callers expecting duplicates (unlikely).Overview
Fixes duplicate in-app notifications by adding an
id-based dedupe guard toNotificationsCache.unshift(), skipping the prepend when the cache already contains that notification.Adds a comprehensive
notifications-cache.test.tssuite, including new cases asserting the dedupe behavior and validating cache clearing, aggregation, unique retrieval, and event-driven update/remove emissions.Reviewed by Cursor Bugbot for commit 89bad2e. Configure here.