Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected use of 'self' no-restricted-globals React

Trying to write a Service Worker for my PWA app, caugth this error. I used Google/Mozilla samples for service workers, but, anyway.

var CACHE_NAME = 'test-cache';
var urlsToCache = [
    '/'
];

self.addEventListener('install', function (event) {
    event.waitUntil(
        caches.open(CACHE_NAME)
            .then(function (cache) {
                console.log('Opened cache');
                return cache.addAll(urlsToCache);
            })
    );
});
like image 809
PandaTheSlayer Avatar asked May 31 '17 18:05

PandaTheSlayer


3 Answers

Use window.self instead of self.

like image 75
Grzegorz Brzęczyszczykiewicz Avatar answered Nov 10 '22 23:11

Grzegorz Brzęczyszczykiewicz


You can explicitly remove self from the no-restricted-globals rule or simply disable the rule for the line containing self using eslint-disable-line or eslint-disable-next-line:

self.addEventListener('install', function (event) { /* eslint-disable-line no-restricted-globals */
...

Or

/* eslint-disable-next-line no-restricted-globals */
self.addEventListener('install', function (event) {
...
like image 33
Fraction Avatar answered Nov 11 '22 01:11

Fraction


Add var self = this; or use this.addEventListener(...)

like image 3
dparkar Avatar answered Nov 11 '22 01:11

dparkar