Skip to content

fix(sw-update): dwell timer + debug instrumentation against background reload loop#2034

Merged
koala73 merged 1 commit intomainfrom
fix/sw-update-dwell-timer-instrumentation
Mar 22, 2026
Merged

fix(sw-update): dwell timer + debug instrumentation against background reload loop#2034
koala73 merged 1 commit intomainfrom
fix/sw-update-dwell-timer-instrumentation

Conversation

@koala73
Copy link
Owner

@koala73 koala73 commented Mar 22, 2026

Summary

  • Dwell timer (5 s): auto-reload on tab-hide only arms after the tab has been visible for VISIBLE_DWELL_MS continuously since the toast appeared. Prevents the infinite background-tab reload loop (hidden tab → SW update detected → onHidden fires immediately → reload → still hidden → same cycle) and session-restore ghost reloads.
  • Rolling debug log: opt-in via localStorage.setItem('wm-debug-sw', '1'). Stores up to 30 timestamped entries in sessionStorage['wm-sw-debug-log'] covering the full toast lifecycle. Useful for diagnosing future SW reload reports post-mortem.
  • __APP_VERSION__ wired into the handler so log entries carry the deployed build version.

Test plan

  • 19 unit tests pass (node --test tests/sw-update.test.mts)
  • New tests cover: dwell timer start on visible toast, no timer when hidden, timer starts on return to visible, timer cleared on dismiss/reload, no auto-reload before dwell elapses
  • Typecheck clean
  • Deploy a new version → confirm toast appears instead of immediate page reload
  • Leave toast up for 5 s while visible, switch away → confirm page reloads
  • Switch away within 5 s → confirm no reload
  • Dismiss toast, switch away → confirm no reload

@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@vercel
Copy link

vercel bot commented Mar 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
worldmonitor Ready Ready Preview, Comment Mar 22, 2026 5:46am

Request Review

Replaces the simple autoReloadAllowed flag from #2032 with a full 5 s
visible-dwell requirement and opt-in debug logging.

Changes vs main:

Dwell timer (5 s):
  Auto-reload on tab-hide only arms after VISIBLE_DWELL_MS of continuous
  visibility. Hidden time is NOT counted — the timer is cancelled whenever the
  tab goes hidden before expiry and restarts from zero on the next foreground.
  Prevents session-restore ghost reloads and background infinite reload loops.

Debug log (opt-in):
  localStorage.setItem('wm-debug-sw', '1') enables console + sessionStorage
  logging (key: wm-sw-debug-log, rolling 30 entries). Logs the full toast
  lifecycle: handler-installed, controllerchange, toast-shown, dwell-timer-
  started/cancelled/expired, visibility transitions, auto-reload, dismiss.
  __APP_VERSION__ included in every entry via version option in main.ts.

P2 fix:
  When a newer deploy supersedes an existing toast, its pending dwell timer is
  cancelled via currentDwellCancel so it cannot fire after the toast is gone
  and pollute the debug log.

New options: setTimer, clearTimer (test injection), debug, version.
22 tests covering all dwell timer mechanics, all pass.
@koala73 koala73 force-pushed the fix/sw-update-dwell-timer-instrumentation branch from 75b9c70 to 32f31d1 Compare March 22, 2026 05:44
@koala73 koala73 merged commit 6142680 into main Mar 22, 2026
6 checks passed
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