Skip to content

fix(auth): make SessionManager.registerRefreshHook idempotent for HMR#1045

Merged
yahyafakhroji merged 1 commit intomainfrom
fix/session-manager-hmr-safe-hook
Mar 3, 2026
Merged

fix(auth): make SessionManager.registerRefreshHook idempotent for HMR#1045
yahyafakhroji merged 1 commit intomainfrom
fix/session-manager-hmr-safe-hook

Conversation

@yahyafakhroji
Copy link
Contributor

@yahyafakhroji yahyafakhroji commented Mar 3, 2026

Problem

During local development, Vite HMR re-executes entry.ts on every hot reload. The sessionManager singleton survives the reload (module-scoped), but registerRefreshHook() gets called again — hitting the "already registered" guard and throwing an error that crashes the Vite error overlay, blocking the UI until a manual tab reload.

Root Cause

The registerRefreshHook() method was designed to throw on duplicate registration to prevent multiple consumers from eavesdropping on raw access tokens. However, in Vite's HMR cycle, it's the same consumer (entry.ts) re-registering — not a different one.

Fix

Make registerRefreshHook() idempotent — repeated calls replace the previous hook instead of throwing. This is safe because:

  • Only one hook can be active at a time (latest wins)
  • The replacement semantics match HMR expectations (new module version replaces old)
  • Production behavior is unchanged (entry.ts only runs once at server start)

Changes

  • app/utils/auth/session-manager.ts — remove throw guard, replace with simple overwrite
  • cypress/component/session-manager.cy.ts — update test: verify replacement behavior instead of expecting throw

The singleton SessionManager survives Vite HMR, but entry.ts re-executes
on every hot reload — calling registerRefreshHook() again on the same
instance. The previous throw guard caused a crash that blocked the UI
during local development.

Replace the throw with a simple overwrite so repeated registrations
replace the previous hook instead of erroring.
@yahyafakhroji yahyafakhroji requested a review from a team March 3, 2026 04:50
@yahyafakhroji yahyafakhroji merged commit da6bd76 into main Mar 3, 2026
10 checks passed
@yahyafakhroji yahyafakhroji deleted the fix/session-manager-hmr-safe-hook branch March 3, 2026 06:16
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.

2 participants