Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Worker Install fails: "cannot read property 'addAll of undefined"

So I'll keep this succinct: When trying to install, my service worker fails. This is all of my code in sw.js:

var cacheName = 'randomstring';
var filesToCache = [ '/' ];

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

I get an exception because cache is undefined (on the cache.addAll bit).

Not really sure why this is the case?

I've used service workers before and never encountered this issue. This is my first time using a service worker with an ASP.Net back-end though, so not sure if that's the problem?

like image 208
Maverick Avatar asked Jun 07 '17 04:06

Maverick


People also ask

What is the error code for installing service worker?

Service Worker Installation] Installing service worker failed TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. · Issue #375 · OneSignal/OneSignal-Website-SDK · GitHub

When is installation of a service worker attempted?

Installation of the worker is attempted when service worker-controlled pages are accessed subsequently. An Install event is always the first one sent to a service worker (this can be used to start the process of populating an IndexedDB, and caching site assets).

Why is the new version of my service worker not activated?

If your service worker has previously been installed, but then a new version of the worker is available on refresh or page load, the new version is installed in the background, but not yet activated. It is only activated when there are no longer any pages loaded that are still using the old service worker.

Why is the service worker suspended when I refresh the page?

When you refresh the page, the browser tries to install and activate the Service Worker with the new code. Since the latter is different from the active Service Worker, the one that has been registered at the beginning of step 2, the activation of the new one is suspended.


2 Answers

So, I figured this out. I was going to vote to close the question, but I figured I'd leave it here as I saw some other people with this issue who didn't know how to resolve it. Even though it's super-stupid :) (or more accurately, I am).

So I was running the website via the "Play" button, aka "Start Debugging", which, in Visual Studio 2017, launches a special Chrome window, in which the above error will be thrown.

To work around the issue, I can (or you can, internet traveller of the future) simply start without debugging, host the website in IIS, etc.

EDIT: If there's a better workaround where I can use the service worker in debug mode, please suggest it and I'll mark that as the answer. For my specific problem though, the above workaround is fine :).

like image 144
Maverick Avatar answered Oct 20 '22 09:10

Maverick


Encountered the same problem and found some other ways.

VS recognises "chrome.exe" while debugging and adds some parameters, that´s why service workers won´t working.

There is an option Debug => Option => Debugging => General => Enable javascript debugging for asp.net (Chrome, Edge and FireFox). If you don´t want to use js debugging in vs - like me because i use chrome for js debugging - just deactivate this option and service workers will work. VS Enable JS Debugging in Chrome

Alternatively you can add chrome as a new "browser" and switch the browser for debugging. Because vs recognise "chrome.exe" make a symlink via administative commandline "mklink chromedirect.exe chrome.exe" and add it as new browser in visual studio.

This can be done under the "Play" context menu => Browse with. VS Play Context Menu

Just add chromedirect.exe without any arguments and a friendly name like "Google Chrome Direct". After that you can switch to the browsers and select if you want VS JS Debugging or not.

like image 39
Ceffis Avatar answered Oct 20 '22 09:10

Ceffis