Skip to content

perf: defer service worker registration with requestIdleCallback (#461) - #558

Merged
nanaf6203-bit merged 1 commit into
MettaChain:mainfrom
gbengaeben:fix/461-sw-registration-perf
Jun 28, 2026
Merged

perf: defer service worker registration with requestIdleCallback (#461)#558
nanaf6203-bit merged 1 commit into
MettaChain:mainfrom
gbengaeben:fix/461-sw-registration-perf

Conversation

@gbengaeben

Copy link
Copy Markdown
Contributor

Summary

Defers service worker registration using requestIdleCallback to avoid blocking the initial paint and improve Time to Interactive (TTI).

Problem

ServiceWorkerRegistration was eagerly registering the service worker on every page load (via load event or document.readyState === 'complete'), including on routes that don't require offline support. This blocks the main thread and increases TTI.

Solution

  • Added scheduleWhenIdle() helper that uses requestIdleCallback with a 5-second timeout
  • Falls back to setTimeout(2000) when requestIdleCallback is unavailable
  • SW registration is now deferred until the browser is idle
  • startQueueAutoFlush() is still called eagerly (it's lightweight)
  • Properly cancels idle callback on component unmount

Changes

  • ServiceWorkerRegistration.tsx: Added scheduleWhenIdle helper, removed eager register-on-load logic
  • ServiceWorkerRegistration.test.tsx: Rewritten from vitest to jest, added tests for deferred registration, idle callback, fallback to setTimeout, cancellation on unmount, and error handling (10 tests passing)

Closes #461

@drips-wave

drips-wave Bot commented Jun 27, 2026

Copy link
Copy Markdown

@gbengaeben Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Copy link
Copy Markdown
Contributor

Smart perf fix. Merging.

@nanaf6203-bit
nanaf6203-bit merged commit 24dca49 into MettaChain:main Jun 28, 2026
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.

perf: service worker registration blocks initial paint

2 participants