What error are you getting the service worker only working trough https anyways would be beneficial not to include this into your html file but rather in another js file.
Here is something that would call you sw.js file. include this into your html or again another js file.
var deferredPrompt; if (!window.Promise) { window.Promise = Promise; } if ('serviceWorker' in navigator) { navigator.serviceWorker .register('sw.js') .then(function () { console.log('Service worker registered!'); }) .catch(function(err) { console.log(err); }); } window.addEventListener('beforeinstallprompt', function(event) { console.log('beforeinstallprompt fired'); event.prompt(); deferredPrompt = event; return false; });
Try This
self.addEventListener('fetch', function (event) { var url = 'https://httpbin.org/get'; //add your URL here if (event.request.url.indexOf(url) > -1) { event.respondWith( caches.open(CACHE_DYNAMIC_NAME) .then(function (cache) { return fetch(event.request) .then(function (res) { // trimCache(CACHE_DYNAMIC_NAME, 3); cache.put(event.request, res.clone()); return res; }); }) ); } else if (isInArray(event.request.url, STATIC_FILES)) { event.respondWith( caches.match(event.request) ); } else { event.respondWith( caches.match(event.request) .then(function (response) { if (response) { return response; } else { return fetch(event.request) .then(function (res) { return caches.open(CACHE_DYNAMIC_NAME) .then(function (cache) { // trimCache(CACHE_DYNAMIC_NAME, 3); cache.put(event.request.url, res.clone()); return res; }) }) .then(function (err) { return caches.open(CACHE_STATIC_NAME) .then(function (cache) { if (event.request.headers.get('accept').includes('text/html')) { return cache.match('offline.html'); } }); }); } }) ); } }); self.addEventListener('install', function (event) { console.log('[Service Worker] Installing Service Worker ...', event); event.waitUntil( caches.open(CACHE_STATIC_NAME) .then(function (cache) { console.log('[Service Worker] Precaching App Shell'); cache.addAll(STATIC_FILES); }) ) });