-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
139 lines (125 loc) · 4.3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//缓存名称
const CACHE_NAME = 'demo-pwaaaa'; //缓存名称
// 缓列举要默认缓存的静态资源,一般用于离线使用
const CACHE_URLS = [
'/',
'/index.html',
'/app.css',
'/manifest.json',
'/img/pic1.jpg',
'/img/pic2.jpg',
'/img/pic3.jpg'
];
self.addEventListener('install', event => {
// event.waitUtil 用于在安装成功之前执行一些预装逻辑
// 但是建议只做一些轻量级和非常重要资源的缓存,减少安装失败的概率
// 安装成功后 ServiceWorker 状态会从 installing 变为 installed
event.waitUntil(
// 使用 cache API 打开指定的 cache 文件
caches.open(CACHE_NAME).then(cache => {
// 添加要缓存的资源列表
return cache.addAll(CACHE_URLS)
})
.then(self.skipWaiting()) // 安装阶段跳过等待,直接进入 active
);
});
// // 联网状态下执行
// function onlineRequest(fetchRequest) {
// // 使用 fecth API 获取资源,以实现对资源请求控制
// return fetch(fetchRequest).then(response => {
// // 在资源请求成功后,将 image、js、css 资源加入缓存列表
// if (!response ||
// response.status !== 200 ||
// !response.headers.get('Content-type').match(/image|javascript|test\/css/i)
// ) {
// return response;
// }
// const responseToCache = response.clone();
// caches.open(CACHE_NAME)
// .then(function (cache) {
// cache.put(event.request, responseToCache);
// });
// return response;
// }).catch(() => {
// // 获取失败,离线资源降级替换
// offlineRequest(fetchRequest);
// });
// }
// 离线状态下执行,降级替换
// function offlineRequest(request) {
// // 使用离线图片
// if (request.url.match(/\.(png|gif|jpg)/i)) {
// return caches.match('/images/offline.png');
// }
// // 使用离线页面
// if (request.url.match(/\.html$/)) {
// return caches.match('/test/offline.html');
// }
// }
// self.addEventListener('fetch', event => {
// event.respondWith(
// caches.match(event.request)
// .then(hit => {
// // 返回缓存中命中的文件
// if (hit) {
// return hit;
// }
// const fetchRequest = event.request.clone();
// if (navigator.online) {
// // 如果为联网状态
// return onlineRequest(fetchRequest);
// } else {
// // 如果为离线状态
// return offlineRequest(fetchRequest);
// }
// })
// );
// });
self.addEventListener('fetch', event => {
// 拦截网络请求,先在缓存文件中查找
if (event.request.url.startsWith(self.location.origin)) {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
// 返回缓存中命中的文件
if (cachedResponse) {
return cachedResponse;
}
// 使用 fecth API 获取资源,以实现对资源请求控制
return fetch(event.request.clone()).then(response => {
return response;
});
})
);
}
});
self.addEventListener('activate', event => event.waitUntil(
Promise.all([
// 更新客户端
clients.claim(),
// 清理旧版本
caches.keys().then(cacheList => Promise.all(
cacheList.map(cacheName => {
if (cacheName !== CACHE_NAME) {
caches.delete(cacheName);
}
})
))
])
));
// 监听推送事件 然后显示通知
self.addEventListener('push', function (event) {
const title = 'Push Codelab';
const options = {
body: 'Yay it works.',
icon: 'img/48.png',
badge: 'img/48.png'
};
event.waitUntil(self.registration.showNotification(title, options));
});
// 监听通知的点击事件
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(
clients.openWindow('https://developers.google.com/web/') // eslint-disable-line
);
});