Track 07 · Lesson 12 of 18Identity and login

Track 07 · Identity and login

Five tabs, one refresh

Lesson 12 of 18· 6 min

Users open many tabs. When a laptop wakes up, every tab finds an expired token at the same moment, and each fires several requests. If refresh tokens rotate, two refreshes racing with the same token can kill each other, and log the user out for having tabs open.

Wake the laptop

Then switch the tools off one by one

  • One shared promise per tab: 3 requests, 1 refresh.
  • Only one tab refreshes at a time.
  • Inside the lock: if the expiry changed, someone already refreshed. Skip.
  • The tab that refreshed pushes the new token to all others.

Tab 13 req

refresh → server

Tab 23 req

skipped

Tab 33 req

skipped

Tab 43 req

skipped

Tab 53 req

skipped
1refresh call hit the server
Ideal. One refresh reaches the server; every other tab and request reuses its token. This is the recommended setup.

Four tools, four jobs#

  • Single-flight (per tab): one shared promise, so three requests in one tab cause one refresh.
  • Web Locks (across tabs): navigator.locks lets only one tab refresh at a time. Each tab is its own JavaScript world, so per-tab locks don’t help here.
  • Expiry double-check: inside the lock, check again. If the token’s expiry changed, someone already refreshed: skip the network.
  • BroadcastChannel: the tab that refreshed pushes the new token to all the others.

Classic cookie sessions don’t have this problem; every token-based setup does.

Check yourself

Five tabs wake up at once, each firing 3 requests. Ideally, how many refresh calls reach the server?