Service Worker Install error: "cannot read property" addAll from undefined "

So, I will keep it in a tight state: when I try to install, my office worker fails. This is all 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 (per cache.addAll bit).

Not quite sure why this is?

I have used service workers before and have never encountered this problem. This is the first time I've used an ASP.Net-enabled service, but not sure what the problem is?

+5
source share
2 answers

So, I figured it out. I was going to vote to close the question, but I decided that I would leave it here when I saw other people with this question who did not know how to resolve it. Although this is super-stupid :) (or rather, me).

So, I launched the website using the Play button, as well as the Start Debugging button, which in Visual Studio 2017 launches a special Chrome window in which the above error will be displayed.

To get around the problem, I can (or you can, the Internet traveler of the future) simply start without debugging, host the website in IIS, etc.

EDIT: If there is a better way around the path where I can use the service worker in debug mode, suggest it and I will mark this as an answer. However, for my specific problem, the above solution works fine :).

+4
source

The same problem was detected and other methods found.

VS recognizes "chrome.exe" during debugging and adds some parameters, so working services do not work.

There is an option Debug => Option => Debugging => General => Enable javascript debugging for asp.net (chrome, etc.). If you do not want to use js debugging in vs - like me, because I use chrome to debug js - just disable this option and the 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. Since vs recognizes "chrome.exe", create a symbolic link through the administrative command line "mklink chromedirect.exe chrome.exe" and add it as a new browser in visual studio.

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

Just add chromedirect.exe without any arguments and friendly name, for example "Google Chrome Direct". After that, you can switch to the browser and choose whether you want to debug VS JS or not.

+1
source

Source: https://habr.com/ru/post/1268614/


All Articles