Completed
Pull Request — develop (#226)
by Xaver
31s
created

service-worker.js ➔ ... ➔ caches.then   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 7
rs 9.4285
nop 1
1
var CACHE = 'v05';
2
3
self.addEventListener('install', function (evt) {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
  console.log('The service worker is being installed.');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
5
  evt.waitUntil(caches.open(CACHE).then(function (cache) {
0 ignored issues
show
Bug introduced by
The variable caches seems to be never declared. If this is a global, consider adding a /** global: caches */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
    cache.addAll([
7
      '',
8
      'index.html',
9
      'app.js',
10
      'logo.svg',
11
      'fonts/*'
12
    ]);
13
  }));
14
});
15
16
self.addEventListener('fetch', function (evt) {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
17
  if (/\/browser-sync\//.exec(evt.request.url) !== null) {
18
    return;
19
  }
20
21
  if (/meshviewer\.json/.exec(evt.request.url) !== null) {
22
    // foo
23
  }
24
  evt.respondWith(fromCache(evt.request));
25
  evt.waitUntil(
26
    update(evt.request)
27
      .then(refresh)
28
  );
29
});
30
31
function fromCache(request) {
32
  return caches.open(CACHE).then(function (cache) {
0 ignored issues
show
Bug introduced by
The variable caches seems to be never declared. If this is a global, consider adding a /** global: caches */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
33
    return cache.match(request);
34
  });
35
}
36
37
function update(request) {
38
  return caches.open(CACHE).then(function (cache) {
0 ignored issues
show
Bug introduced by
The variable caches seems to be never declared. If this is a global, consider adding a /** global: caches */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
39
    return fetch(request).then(function (response) {
40
      return cache.put(request, response.clone()).then(function () {
41
        return response;
42
      });
43
    });
44
  });
45
}
46
47
// Sends a message to the clients.
48
function refresh(response) {
49
  return self.clients.matchAll().then(function (clients) {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
    clients.forEach(function (client) {
51
      // Encode which resource has been updated. By including the
52
      // [ETag](https://en.wikipedia.org/wiki/HTTP_ETag) the client can
53
      // check if the content has changed.
54
      var message = {
55
        type: 'refresh',
56
        url: response.url,
57
        // Notice not all servers return the ETag header. If this is not
58
        // provided you should use other cache headers or rely on your own
59
        // means to check if the content has changed.
60
        eTag: response.headers.get('ETag')
61
      };
62
      // Tell the client about the update.
63
      client.postMessage(JSON.stringify(message));
64
    });
65
  });
66
}
67
68
self.addEventListener('activate', function (event) {
0 ignored issues
show
Bug introduced by
The variable self seems to be never declared. If this is a global, consider adding a /** global: self */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
69
  var cacheWhitelist = [CACHE];
70
71
  event.waitUntil(
72
    caches.keys().then(function (keyList) {
0 ignored issues
show
Bug introduced by
The variable caches seems to be never declared. If this is a global, consider adding a /** global: caches */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
73
      return Promise.all(keyList.map(function (key) {
74
        if (cacheWhitelist.indexOf(key) === -1) {
75
          return caches.delete(key);
0 ignored issues
show
Bug introduced by
The variable caches seems to be never declared. If this is a global, consider adding a /** global: caches */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
76
        }
77
        return false;
78
      }));
79
    })
80
  );
81
});
82