Setting up a Basic Service Worker

Service Workers are a relatively new, but incredibly powerful, technology. A service worker is a JavaScript file that runs independently to a web page and can perform specific services such as background sync, push notifications, handling network requests, and caching. Because of its independence, it does not have the access to the DOM a typical JavaScript file does, but this allows it to be fully asynchronous and therefore non-blocking.

Because service workers can be used in a variety of circumstances for many different tasks, they can get quite complicated. So, to get started, I thought I would show how to setup a basic service worker to handle something simple, caching. I also decided to try something a bit different and made it a video tutorial instead of a regular article. Here it is -

Setting up a Basic Service Worker

And this was the service-worker.js we setup in the tutorial -

// Set a name for the current cache
var cacheName = 'v1';

// Default files to always cache
var cacheFiles = [

self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Installed');

// e.waitUntil Delays the event until the Promise is resolved

// Open the cache {

// Add all the default files to the cache
console.log('[ServiceWorker] Caching cacheFiles');
return cache.addAll(cacheFiles);
); // end e.waitUntil

self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activated');


// Get all the cache keys (cacheName)
caches.keys().then(function(cacheNames) {
return Promise.all( {

// If a cached item is saved under a previous cacheName
if (thisCacheName !== cacheName) {

// Delete that cached file
console.log('[ServiceWorker] Removing Cached Files from Cache - ', thisCacheName);
return caches.delete(thisCacheName);
); // end e.waitUntil


self.addEventListener('fetch', function(e) {
console.log('[ServiceWorker] Fetch', e.request.url);

// e.respondWidth Responds to the fetch event

// Check in cache for the request being made

.then(function(response) {

// If the request is in the cache
if ( response ) {
console.log("[ServiceWorker] Found in Cache", e.request.url, response);
// Return the cached version
return response;

// If the request is NOT in the cache, fetch and cache

var requestClone = e.request.clone();
.then(function(response) {

if ( !response ) {
console.log("[ServiceWorker] No response from fetch ")
return response;

var responseClone = response.clone();

// Open the cache {

// Put the fetched response in the cache
cache.put(e.request, responseClone);
console.log('[ServiceWorker] New Data Cached', e.request.url);

// Return the response
return response;

}); // end

.catch(function(err) {
console.log('[ServiceWorker] Error Fetching & Caching New Data', err);

}) // end caches.match(e.request)
); // end e.respondWith

Full Source Code | Live Demo

This is the first video tutorial like this I've done, so excuse my nervousness! I would also love to hear any feedback you have on the video; if you found it helpful or ways I could improve on it.

Keep in touch KeepinTouch

Subscribe to my Newsletter 📥

Receive quality articles and other exclusive content from myself. You’ll never receive any spam and can always unsubscribe easily.

Elsewhere 🌐