Thursday, 6 June 2019

Service worker not properly working or activating

service worker code:-if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('/serviceWorker.js', { scope: "/public/login.html" }) .then(function() { console.log('CLIENT: service worker registration complete.'); }, function() { console.log('CLIENT: service worker registration failure.'); }); }); } // Files to cache var cacheName = 'overlord-v1'; // represent files to be cached var appShellFiles = [ '/', 'public/customFont.woff2', 'public/fontIcon.css', 'https://shadowlake.herokuapp.com/public/home.html', 'public/jquery.min.js', 'public/login.html', 'public/materialize.css', 'public/materialize.js', 'public/materialize.min.css', 'public/materialize.min.js', 'public/photo-house.jpg', 'public/snow_valley.jpg', ]; var contentToCache = appShellFiles; // Installing Service Worker self.addEventListener('install', function(e) { alert("install"); console.log('[Service Worker] Install'); e.waitUntil( caches.open(cacheName).then(function(cache) { console.log('[Service Worker] Caching all: app shell and content'); return cache.addAll(contentToCache); }) ); }); // activation self.addEventListener('activate', function (event) { alert("activate") event.waitUntil( caches /* This method returns a promise which will resolve to an array of available cache keys. */ .then(function() { const name = localStorage.getItem("username"); const roomkey = localStorage.getItem("roomId"); setCookie("username", name); setCookie("roomId", roomkey); console.log('WORKER: activate completed.'); document.location.href = "/public/home.html" }) ); }); // Fetching content using Service Worker self.addEventListener('fetch', function(e) { alert("fetch") e.respondWith( caches.match(e.request).then(function(r) { console.log('[Service Worker] Fetching resource: '+e.request.url); const name = localStorage.getItem("username") const roomkey = localStorage.getItem("roomId") setCookie("username", name) setCookie("roomId", roomkey) return r || fetch(e.request).then(function(response) { return caches.open(cacheName).then(function(cache) { console.log('[Service Worker] Caching new resource: '+e.request.url); cache.put(e.request, response.clone()); return response; }); }); }) ); }); function setCookie(name,value) { localStorage.setItem(name, value) var expires = ""; document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { document.cookie = name+'=; Max-Age=-99999999;'; } manifest.json​{ "dir": ".", "lang": "en-us", "name": "WildShot", "display": "browser", "start_url": "/public/login.html", "short_name": "overlord", "theme_color": "transparent", "description": "A fun game", "orientation": "any", "background_color": "transparent", "related_applications": [], "prefer_related_applications": false, "icons": [] } I have linked service worker and manifest.json to the starting page of website. When I turn off my internet. no page is loaded and error for internet connection is displayed. What should I do?

Submitted June 06, 2019 at 01:16PM by directorOverride

No comments:

Post a Comment