File size: 2,577 Bytes
f8972e7
 
cc712f9
f8972e7
510eafa
f8972e7
 
 
 
510eafa
 
 
 
 
 
 
 
 
084200d
 
f8972e7
 
 
 
c7a1426
 
7ab4189
f8972e7
 
510eafa
f8972e7
 
 
 
 
 
510eafa
c7a1426
 
7ab4189
 
 
 
f8972e7
 
 
7ab4189
 
 
f8972e7
7ab4189
 
cc712f9
7ab4189
f8972e7
 
 
 
757946c
 
f8972e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510eafa
 
f8972e7
 
510eafa
f8972e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const INDEX_CACHE = 'index-cache-v-' + new Date().getTime(); // index.htmlは毎回更新
const VIDEO_CACHE = 'static-video-cache'; // 動画は一度キャッシュして再利用

const INDEX_ASSETS = [
  '/',
  '/index.html'
];

const VIDEO_ASSETS = [
  '/videos/k-back-human.mp4',
  '/videos/k-back-stick-human.mp4',
  '/videos/k-back-stick.mp4',
  '/videos/k-human.mp4',
  '/videos/k-stick-human.mp4',
  '/videos/m-back-human.mp4',
  '/videos/m-back-stick-human.mp4',
  '/videos/m-back-stick.mp4',
  '/videos/m-human.mp4',
  '/videos/m-stick-human.mp4',
  '/videos/e-back-human.mp4',
  '/videos/e-back-stick-human.mp4',
  '/videos/e-back-stick.mp4',
  '/videos/e-human.mp4',
  '/videos/e-stick-human.mp4'
];

self.addEventListener('install', event => {
  self.skipWaiting();

  event.waitUntil(
    Promise.all([
      caches.open(INDEX_CACHE)
        .then(cache => cache.addAll(INDEX_ASSETS.map(url => new Request(url, { cache: 'reload' })))),
      caches.open(VIDEO_CACHE)
        .then(cache => cache.addAll(VIDEO_ASSETS))
    ])
  );
});

self.addEventListener('activate', event => {
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return Promise.all(
        cacheNames.map(name => {
          if (name !== INDEX_CACHE && name !== VIDEO_CACHE) {
            return caches.delete(name);
          }
        })
      );
    }).then(() => self.clients.claim())
  );
});

self.addEventListener('fetch', event => {
  const url = new URL(event.request.url);

  // 外部リソースはキャッシュ対象外
  if (url.origin !== self.location.origin) {
    return;
  }

  if (url.pathname === '/' || url.pathname.endsWith('index.html')) {
    // 毎回新しい index.html をキャッシュから取得
    event.respondWith(
      caches.open(INDEX_CACHE)
        .then(cache => cache.match(event.request)
          .then(response => response || fetch(event.request)))
    );
    return;
  }

  if (url.pathname.startsWith('/videos/')) {
    // 動画は static-video-cache から
    event.respondWith(
      caches.match(event.request)
        .then(response => response || fetch(event.request)
          .then(networkResponse => {
            return caches.open(VIDEO_CACHE).then(cache => {
              cache.put(event.request, networkResponse.clone());
              return networkResponse;
            });
          }))
    );
    return;
  }

  // その他リクエスト
  event.respondWith(
    caches.match(event.request)
      .then(response => response || fetch(event.request))
      .catch(() => caches.match('/offline.html'))
  );
});