Skip to content

fix: update drafts badge when drafts change in another tab - #138

Open
palmoni5 wants to merge 1 commit into
NodeBB:mainfrom
palmoni5:fix/drafts-badge-cross-tab
Open

fix: update drafts badge when drafts change in another tab#138
palmoni5 wants to merge 1 commit into
NodeBB:mainfrom
palmoni5:fix/drafts-badge-cross-tab

Conversation

@palmoni5

@palmoni5 palmoni5 commented Sep 6, 2026

Copy link
Copy Markdown

Problem

The drafts badge in the sidebar (and the bottom bar on mobile) goes stale across tabs. Save or delete a draft in one tab, and every other open tab keeps showing the old count until the page is reloaded.

Notifications don't have this problem because the server pushes `event:notifications.updateCount` over socket.io to every connected tab. Drafts, however, live entirely in `localStorage` on the client. `setupDrafts()` only refreshes the badge on page load and on the composer's `action:composer.drafts.save` / `action:composer.drafts.remove` jQuery events, which are per-tab.

Fix

Listen for the `storage` event on `window`. Browsers fire it in every other tab of the same origin whenever `localStorage` is written. The badge is refreshed when the `drafts:available` list (the key `drafts.getAvailableCount()` derives from) changes, or when storage is cleared (`key === null`).

Guests are unaffected: their drafts are in `sessionStorage`, which is per-tab and does not fire the event. The dropdown list itself needs no change since it is re-rendered on every `shown.bs.dropdown`.

Test

  1. Log in, open the forum in two tabs.
  2. In tab A, start a new topic/reply and type something so a draft gets saved.
  3. Switch to tab B without reloading: the drafts badge now shows the updated count.
  4. Delete the draft from the drafts dropdown in tab A; tab B's badge updates/hides.

Drafts are stored in localStorage and the sidebar badge was only refreshed
from the composer's in-page save/remove events, so saving or deleting a
draft in one tab left the count stale in every other open tab until a
reload. Listen for the window storage event (fired in other tabs on
localStorage writes) and refresh the badge when the drafts:available list
changes or storage is cleared.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant