Skip to content

Latest commit

 

History

History
116 lines (84 loc) · 6.75 KB

service-workers.md

File metadata and controls

116 lines (84 loc) · 6.75 KB

A Service Worker is a type of web worker. It's a JavaScript file that can control the web page/site it is associated with, intercepting and modifying navigation and resource requests, and caching resources in a very granular fashion to complete offline experiences, or to boost performance.

How Service Workers Work: A Service Worker works by acting as a network proxy between your web application and the network. It's a type of web worker, which means it runs in a separate thread from the main browser thread, so it doesn't have access to the DOM and runs independently of the application it's associated with.

Let's dive into the lifecycle of a Service Worker:

  1. Registration: The first step is to register the Service Worker. This is typically done in your application's JavaScript code. During registration, the browser will start the installation process in the background.
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js')
  .then(function(registration) {
    console.log('Service Worker registered with scope:', registration.scope);
  })
  .catch(function(error) {
    console.log('Service Worker registration failed:', error);
  });
}
  1. Installation: During the installation phase, the Service Worker will typically cache some static assets. If all the files are cached successfully, then the Service Worker becomes installed. If any of the files fail to download and cache, then the installation fails and the Service Worker won't be activated.
self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open('my-cache').then(function(cache) {
      return cache.addAll([
        '/page1/',
        '/page2/',
        '/page3/'
      ]);
    })
  );
});
  1. Activation: Once a Service Worker is installed, it will then activate. This is a good time to manage old caches.
self.addEventListener('activate', function(event) {
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cacheName) {
          // Return true if you want to remove this cache,
          // but remember that caches are shared across
          // the whole origin
        }).map(function(cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
});
  1. Fetch: After the Service Worker activates, it will then take control of the page and start to receive fetch events. This is where the Service Worker can respond to network requests. It can serve a cached response, fetch a response from the network, or even construct a response itself.
self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(response) {
      // Cache hit - return response
      if (response) {
        return response;
      }
      return fetch(event.request);
    })
  );
});
  1. Idle/Redundant: When not in use, a Service Worker goes into an idle state and it's terminated to save memory. It's restarted as needed. If a new Service Worker is installed and activated, the old Service Worker becomes redundant.

In conclusion: Service Workers run separately from the web page and communicate with the pages they control by responding to messages sent via the postMessage interface. They are capable of intercepting network requests, caching or retrieving resources from the cache, and delivering push messages.

Caching Strategies

  1. Cache First, Network Fallback (Cache Falling Back to Network): In this strategy, the Service Worker first tries to fetch the resource from the cache. If the resource is not in the cache, it then fetches it from the network and caches it for future use. This strategy is useful for static assets that don't change often, like CSS, JavaScript, and images.

  2. Network First, Cache Fallback (Network Falling Back to Cache): The Service Worker first tries to fetch the resource from the network. If the network request fails (for example, because the user is offline), it then fetches the resource from the cache. This strategy is useful for data that updates frequently, like news articles or scores in a sports app.

  3. Cache and Network Race (Fastest): The Service Worker tries to fetch the resource from both the cache and the network simultaneously. Whichever returns first is used, and if the network returns the resource after the cache, the cache is updated for future use. This strategy provides the fastest response, but it can waste bandwidth if the network request completes after the cached response has been returned.

  4. Cache Only: The Service Worker only fetches the resource from the cache. If the resource is not in the cache, the request fails. This strategy is useful for offline-first scenarios.

  5. Network Only: The Service Worker only fetches the resource from the network. If the network request fails, the request fails. This strategy is useful for non-GET requests that can't be cached, like a POST request to update data on the server.

Use Cases for Service Workers:

  1. Offline Mode: Service Workers can intercept network requests and serve cached responses when the network is not available, enabling the application to work offline.

  2. Background Sync: Service Workers can synchronize data in the background, even when the user is not interacting with the application.

  3. Push Notifications: Service Workers can listen for push events from the server and display notifications to the user, even when the application is not open in the browser.

  4. Performance Improvements: By caching resources, Service Workers can reduce the load on the server and improve the performance of the application.

Pros of Service Workers:

  1. Offline Capabilities: One of the biggest advantages of Service Workers is the ability to provide offline functionality. This can significantly improve the user experience, especially in areas with poor network connectivity.

  2. Improved Performance: By caching resources and serving them directly from the cache, Service Workers can reduce network traffic and load times.

  3. Background Sync: Service Workers can synchronize data in the background, ensuring that the application data is always up-to-date.

  4. Push Notifications: Service Workers allow web applications to receive push notifications, similar to native apps.

Cons of Service Workers:

  1. Complexity: Implementing Service Workers can be complex and requires a good understanding of the lifecycle events and caching strategies.

  2. Compatibility: Not all browsers support Service Workers. However, most modern browsers do.

  3. Security: Service Workers can only be used over HTTPS (except for localhost for development purposes) because they can intercept network requests and modify responses.