Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Worker not registering on gh-pages hosted website

The service worker is registered and works fine on the vscode localhost but not on the gh-pages hosted site.

Screenshot of localhost sw.

screenshot of localhhost sw

screenshot of gh-pages sw

As you can see the sw opens the cache but then fails to register with a type error in promise. While sometimes the sw not even opens the cache and fails.

Below is my snippet to install and update the service worker.

var CACHE_NAME = 'dev';
var urlsToCache = [
  '/',
  '/styles/style.css',
];


// Installing a sevice worker and defining files to be cached. 
self.addEventListener('install', function(event) {
  // Perform install steps
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});


// Updating the service worker.
self.addEventListener('fetch', function(event) {
    event.respondWith(
      caches.match(event.request)
        .then(function(response) {
          // Cache hit - return response
          if (response) {
            return response;
          }
  
          // IMPORTANT: Clone the request. A request is a stream and
          // can only be consumed once. Since we are consuming this
          // once by cache and once by the browser for fetch, we need
          // to clone the response.
          var fetchRequest = event.request.clone();
  
          return fetch(fetchRequest).then(
            function(response) {
              // Check if we received a valid response
              if(!response || response.status !== 200 || response.type !== 'basic') {
                return response;
              }
  
              // IMPORTANT: Clone the response. A response is a stream
              // and because we want the browser to consume the response
              // as well as the cache consuming the response, we need
              // to clone it so we have two streams.
              var responseToCache = response.clone();
  
              caches.open(CACHE_NAME)
                .then(function(cache) {
                  cache.put(event.request, responseToCache);
                });
  
              return response;
            }
          );
        })
      );
  });


Below is my snippet to register the service worker.
 <!-- Service worker -->
    <script>
        if('serviceWorker' in navigator) {
          navigator.serviceWorker
                .register('sw.js')
                .then(function() { console.log("Service Worker Registered"); });
        }
    </script>
like image 459
soda Avatar asked May 27 '18 13:05

soda


Video Answer


1 Answers

Your

var urlsToCache = [
  '/',
  '/styles/style.css',
];

doesn't correspond to the actual URLs that you intend to cache when you're in the GitHub Pages environment.

Instead of absolute URLs that start with /, you should use relative URLs, since all your resources are under a subdirectory of your GitHub Pages' root.

var urlsToCache = [
  './',
  './styles/style.css',
];

Should work in both localhost and in your GitHub Pages environments.

like image 181
Jeff Posnick Avatar answered Oct 17 '22 04:10

Jeff Posnick