Course contentsShow
← Web Development
Lesson 2437 of 2,98243. Service Workers Deep DiveFree lesson

Service Worker Lifecycle Events

Debug install, activate, and fetch events with console logging and breakpoints to understand worker behavior.

Service Worker Lifecycle Events

What you'll learn: How to debug install, activate, and fetch events with console logging and breakpoints to understand exactly when and why your service worker executes code.

Why Debugging Lifecycle Events Matters

Service workers operate in the background, making their behavior invisible unless you actively observe it. The three core lifecycle events—install, activate, and fetch—are your primary windows into understanding when your worker takes control, updates caches, and intercepts network requests.

Debugging with Console Logging

Add strategic console.log() statements at the start of each event handler:

self.addEventListener('install', (event) => {
  console.log('[SW] Install event triggered', event);
  // Your install logic here
});

self.addEventListener('activate', (event) => {
  console.log('[SW] Activate event triggered', event);
  // Your activation logic here
});

self.addEventListener('fetch', (event) => {
  console.log('[SW] Fetch intercepted:', event.request.url);
  // Your fetch logic here
});

The [SW] prefix helps you filter service worker logs in DevTools. Log the event object to inspect timing, related promises, and state.

Using Breakpoints in DevTools

Open Chrome DevTools → Sources → find your service worker file. Click line numbers to set breakpoints inside event listeners. When events fire, execution pauses, letting you step through code, inspect variables, and watch cache operations in real-time.

Combine this with the Application panel's Service Worker section to trigger updates manually and observe state transitions.

What You'll Discover

You'll see that install fires once when the worker first downloads, activate runs after skipWaiting() or when old workers are gone, and fetch fires for every network request the worker controls.

Key Takeaway: Console logging and breakpoints inside install, activate, and fetch events transform service worker debugging from guesswork into precise observation of lifecycle timing and behavior.