Updating website to a Progressive Web App

Viewed 68

I want to upgrade my website to a PWA(Progressive Web App). For this I already implemented some things in my code:

index.html

<script>
      if('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/sw.js')
          .then(function() {
                console.log('Service Worker Registered');
          });
      }
    </script>  

sw.js

var cacheName = 'kaffboard_alpha';
var filesToCache = [
  '/',
  '/index.html',
  '/index.js',
  '/style.css'
];

self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
  e.waitUntil(
    caches.open(cacheName).then(function(cache) {
      console.log('[ServiceWorker] Caching app shell');
      return cache.addAll(filesToCache);
    })
  );
});

self.addEventListener('activate',  event => {
  event.waitUntil(self.clients.claim());
});

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

Unfortunately, I got these two errors:

error in console I think only the second error is caused by the SW. I use Firebase Hosting. After I deploy the site it even does not work offline.

1 Answers

The two errors are related to the same problem.

Status code 404 means the file has not been found. Your uncaught promise error is that it can't find the suggested resource and there is no catch mechanism in place.

Check your directory isn't nested in another folder structure.

Related