質問するログイン新規登録

質問編集履歴

1

ソースを追加

2020/09/16 05:25

投稿

otftrough
otftrough

スコア477

title CHANGED
File without changes
body CHANGED
@@ -6,4 +6,70 @@
6
6
 
7
7
  同じやりかたでも、index.htmlはキャッシュされます。
8
8
 
9
- なにか特殊な方法が必要でしょうか?
9
+ なにか特殊な方法が必要でしょうか?
10
+
11
+
12
+ ---
13
+ **追記**
14
+
15
+ index.htmlでのServiceWorkerの登録は下記のようになっています。
16
+ ```JacaScript
17
+
18
+ if ('serviceWorker' in navigator) {
19
+ navigator.serviceWorker.register('sw.js')
20
+ .then(reg => {
21
+ console.log('Service worker registered.', reg);
22
+ reg.onupdatefound = () => {
23
+ console.log('cash update.');
24
+ reg.update();
25
+ }
26
+ if (typeof reg.update == 'function') {
27
+ console.log('cash update.');
28
+ reg.update();
29
+ }
30
+ });
31
+ } else console.log('Service worker can\'t registered.');
32
+ ```
33
+
34
+ sw.jsの中身です。
35
+ ```JacaScript
36
+ var CACHE_NAME = "otft-info-20200914";
37
+ var urlsToCache = [
38
+ "/",
39
+ "index.html",
40
+ "info/otftnamelogo.png",
41
+ "info/note.svg",
42
+ "info/beat01.png",
43
+ "info/LogIcon.png"
44
+ ];
45
+
46
+ self.addEventListener('install', function(event) {
47
+ event.waitUntil(
48
+ caches
49
+ .open(CACHE_NAME)
50
+ .then(function(cache){
51
+ return cache.addAll(urlsToCache);
52
+ })
53
+ );
54
+ });
55
+
56
+ self.addEventListener('fetch', function(e) {
57
+ });
58
+
59
+ self.addEventListener('activate', function(event) {
60
+ event.waitUntil(
61
+ caches.keys().then(function(cacheNames) {
62
+ return Promise.all(
63
+ cacheNames.filter(function(cacheName) {
64
+ return cacheName !== CACHE_NAME;
65
+ }).map(function(cacheName) {
66
+ console.info("delete cache: " + cacheName);
67
+ return caches.delete(cacheName);
68
+ })
69
+ );
70
+ }).then(() => {
71
+ clients.Claim();
72
+ })
73
+ );
74
+ });
75
+ ```