Skip to content

Fix PWA energy kills with visibility-aware background pausing#36

Merged
selfcontained merged 3 commits into
mainfrom
worktree-pwa-energy-fixes
Mar 11, 2026
Merged

Fix PWA energy kills with visibility-aware background pausing#36
selfcontained merged 3 commits into
mainfrom
worktree-pwa-energy-fixes

Conversation

@selfcontained

@selfcontained selfcontained commented Mar 11, 2026

Copy link
Copy Markdown
Owner

Summary

  • Close SSE EventSource when page is hidden, reopen on visible — prevents Safari from seeing sustained network activity in background PWAs
  • Clear health poll interval when hidden (instead of just skipping inside the callback) — eliminates 8s timer fires entirely while backgrounded
  • Guard WebSocket reconnect timers with document.hidden check — prevents reconnect loops from firing in the background
  • Add visibility check to release manager 2s polling — stops aggressive polling when tab is hidden
  • Pause CSS animations when hidden via html[data-hidden] attribute — stops GPU-composited will-change: transform animation from consuming energy
  • Add energy metrics tracker (web/src/lib/energy-metrics.ts) that records SSE events, reconnects, HTTP requests, health poll fires/skips, visibility transitions, and hidden duration. Persists to localStorage every 30s and beacons to /api/v1/energy-report on visibility change to hidden — survives Safari's forced reload for post-mortem diagnostics
  • Add backend /api/v1/energy-report endpoint that logs beacon payloads for server-side inspection
  • Add mandatory pre-completion checks to CLAUDE.md and AGENTS.md — agents must now run type checking, web finalization, e2e tests, and unit tests before marking any task as done

Context

Safari's energy watchdog kills PWAs that consume "significant energy" while backgrounded. The app had several sources of continuous background activity: an always-open SSE connection, 8s health poll timer, WebSocket reconnect loops, and infinite CSS animations with will-change: transform.

Test plan

  • npm run finalize:web passes (TypeScript check + production build)
  • All 5 new e2e tests pass (e2e/energy-visibility.spec.ts)
  • All 22 e2e tests pass (1 pre-existing flaky sidebar width test was also passing at time of final run)
  • Verified data-hidden attribute toggles correctly via Playwright
  • Verified energy metrics persist to localStorage with correct counters
  • Manual PWA test on iOS Safari to confirm the energy kill no longer occurs

🤖 Generated with Claude Code

Brad Harris and others added 3 commits March 11, 2026 09:32
Safari terminates PWAs that consume significant energy in the background.
This adds visibility-aware pausing for SSE, health polling, WS reconnects,
release manager polling, and CSS animations. Also adds an energy metrics
tracker that persists to localStorage and beacons to the backend for
post-mortem diagnostics.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Require agents to run type checking, web finalization, e2e tests, and
unit tests before marking any task as done. Added to both CLAUDE.md
and AGENTS.md to cover Claude Code and Codex agents.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@selfcontained
selfcontained merged commit 5558043 into main Mar 11, 2026
1 check passed
@selfcontained
selfcontained deleted the worktree-pwa-energy-fixes branch April 9, 2026 03:01
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