-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
113 lines (104 loc) · 3.35 KB
/
sw.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const CACHE_VERSION = 'v11';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;
// キャッシュするファイルをセットする
const urlsToCache = [
'.',
'.htaccess',
'camera-viewer.html',
'command-standalone.html',
'index.html',
'data/server.py',
'data/style.css',
'data/Right-Arrow.png',
'data/CodeFont.otf',
'data/CodeFontLight.otf',
'data/MainFont.ttf',
'data/TitleFont.ttf',
'data/VarsionFont.ttf',
'data/bluejelly.js',
'data/CameraLostConnecton.png',
'image/Command.png',
'image/CommandStart.png',
'image/CommandStop.png',
'image/Controle.png',
'image/DisplayRotation.png',
'image/Enter.png',
'image/Error.png',
'image/Fullscreen-Off.png',
'image/Fullscreen-On.png',
'image/Gyro-Off.png',
'image/Gyro-On.png',
'image/icon16.png',
'image/icon32.png',
'image/icon48.png',
'image/icon64.png',
'image/icon150.png',
'image/Light-Off.png',
'image/Light-On.png',
'image/Loading.png',
'image/Question.png',
'image/Setting.png',
'image/StartButton.png',
'image/TitleLogo.png',
'pdf/Mobile-Guide.pdf',
'pdf/Mobile-Setting.pdf',
'pdf/PC-Guide.pdf',
'pdf/PC-Setting.pdf'
];
self.addEventListener('install', (event) => {
event.waitUntil(
// キャッシュを開く
caches.open(CACHE_NAME)
.then((cache) => {
// 指定されたファイルをキャッシュに追加する
return cache.addAll(urlsToCache.map(url => new Request(url, {credentials: 'same-origin'})));
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return cacheNames.filter((cacheName) => {
// このスコープに所属していて且つCACHE_NAMEではないキャッシュを探す
return cacheName.startsWith(`${registration.scope}!`) &&
cacheName !== CACHE_NAME;
});
}).then((cachesToDelete) => {
return Promise.all(cachesToDelete.map((cacheName) => {
// いらないキャッシュを削除する
return caches.delete(cacheName);
}));
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// キャッシュ内に該当レスポンスがあれば、それを返す
if (response) {
return response;
}
// 重要:リクエストを clone する。リクエストは Stream なので
// 一度しか処理できない。ここではキャッシュ用、fetch 用と2回
// 必要なので、リクエストは clone しないといけない
let fetchRequest = event.request.clone();
return fetch(fetchRequest)
.then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
// キャッシュする必要のないタイプのレスポンスならそのまま返す
return response;
}
// 重要:レスポンスを clone する。レスポンスは Stream で
// ブラウザ用とキャッシュ用の2回必要。なので clone して
// 2つの Stream があるようにする
let responseToCache = response.clone();
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
});
})
);
});