codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

36 / 59 topics
34HTML5 History API35HTML5 Web Workers36HTML5 Service Workers37HTML5 Fetch API38HTML5 WebSockets39HTML5 Geolocation API40HTML5 Device Orientation API41HTML5 Payment Request API42HTML5 Push Notifications
Tutorials/HTML & CSS/HTML5 Service Workers
🎨HTML & CSS

HTML5 Service Workers

Updated 2026-04-20
3 min read

Introduction

Service workers are a powerful feature of modern web development, allowing developers to take control of network requests and caching strategies. They enable offline capabilities, background synchronization, push notifications, and more. This tutorial will provide a comprehensive guide to understanding and implementing Service Workers in your HTML & CSS projects.

What is a Service Worker?

A Service Worker is a script that runs in the background, separate from the main JavaScript execution thread of your web application. It acts as a proxy between your web app and the network, enabling you to intercept and handle requests, manage caching strategies, and perform other tasks even when the user is offline.

Key Features

  • Offline Support: Service Workers can cache resources and serve them when the network is unavailable.
  • Background Sync: They can queue up operations to be performed once the network becomes available again.
  • Push Notifications: Service Workers can receive push notifications from servers and display them to users.
  • Custom Network Interception: You can intercept and modify network requests, allowing for advanced caching strategies.

Getting Started with Service Workers

To use a Service Worker in your web application, you need to register it. This is done using the navigator.serviceWorker.register() method. Here's a basic example:

if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    navigator.serviceWorker.register('/service-worker.js')
      .then(registration => {
        console.log('Service Worker registered with scope:', registration.scope);
      })
      .catch(error => {
        console.error('Service Worker registration failed:', error);
      });
  });
}

Service Worker Lifecycle

  1. Installation: The install event is triggered when the service worker is first installed.
  2. Activation: The activate event is triggered when the service worker becomes active and can start controlling clients.
  3. Fetch: The fetch event is triggered whenever a network request is made.

Caching Strategies

Service Workers provide several caching strategies that you can use to optimize your application's performance and offline capabilities. Here are some common strategies:

1. Cache-First Strategy

This strategy serves cached resources if available, otherwise fetches from the network.

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(response => {
        return response || fetch(event.request);
      })
  );
});

2. Network-First Strategy

This strategy fetches resources from the network first, and falls back to the cache if the network request fails.

self.addEventListener('fetch', event => {
  event.respondWith(
    fetch(event.request)
      .then(response => {
        return caches.open('cache-v1').then(cache => {
          cache.put(event.request, response.clone());
          return response;
        });
      })
      .catch(() => {
        return caches.match(event.request);
      })
  );
});

3. Stale-While-Revalidate Strategy

This strategy serves cached resources immediately while fetching a fresh copy from the network in the background.

self.addEventListener('fetch', event => {
  event.respondWith(
    caches.match(event.request)
      .then(response => {
        const fetchPromise = fetch(event.request).then(networkResponse => {
          caches.open('cache-v1').then(cache => {
            cache.put(event.request, networkResponse.clone());
          });
          return networkResponse;
        });

        return response || fetchPromise;
      })
  );
});

Best Practices

  • Versioning: Use version numbers in your cache names to manage updates and clean up old caches.

    const CACHE_NAME = 'cache-v2';
    
    self.addEventListener('install', event => {
      event.waitUntil(
        caches.open(CACHE_NAME)
          .then(cache => {
            return cache.addAll([
              '/',
              '/index.html',
              '/styles.css'
            ]);
          })
      );
    });
    
    self.addEventListener('activate', event => {
      const cacheWhitelist = [CACHE_NAME];
      event.waitUntil(
        caches.keys().then(cacheNames => {
          return Promise.all(
            cacheNames.map(cacheName => {
              if (cacheWhitelist.indexOf(cacheName) === -1) {
                return caches.delete(cacheName);
              }
            })
          );
        })
      );
    });
    
  • Error Handling: Always handle errors gracefully, especially when dealing with network requests.

  • Security: Be cautious about the resources you cache. Avoid caching sensitive data or user-specific information unless absolutely necessary.

Advanced Features

Background Sync

Service Workers can queue up operations to be performed once the network becomes available again. This is useful for tasks like sending form submissions or updating server-side data.

self.addEventListener('sync', event => {
  if (event.tag === 'my-background-sync') {
    event.waitUntil(
      fetch('/api/data', { method: 'POST' })
        .then(response => {
          if (!response.ok) {
            throw new Error('Network request failed');
          }
          return response.json();
        })
    );
  }
});

Push Notifications

Service Workers can receive push notifications from servers and display them to users.

self.addEventListener('push', event => {
  const data = JSON.parse(event.data.text());
  self.registration.showNotification(data.title, {
    body: data.message,
    icon: 'icon.png'
  });
});

Conclusion

Service Workers are a powerful tool for enhancing the performance and capabilities of your web applications. By understanding their lifecycle, caching strategies, and advanced features like background sync and push notifications, you can create robust, offline-capable applications that provide a seamless user experience.

Remember to test your Service Workers thoroughly in different browsers and network conditions to ensure they work as expected. With careful implementation and best practices, Service Workers can significantly improve the reliability and performance of your web applications.


PreviousHTML5 Web WorkersNext HTML5 Fetch API

Recommended Gear

HTML5 Web WorkersHTML5 Fetch API