chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/* haonav PWA Service Worker
|
||||
* 经路由 /haonav/sw.html 输出(作用域 /haonav/)。
|
||||
* 策略:静态资源缓存优先;页面网络优先、离线回退缓存。
|
||||
*/
|
||||
var CACHE = 'haonav-v1';
|
||||
var PRECACHE = [
|
||||
'/static/haonav/css/nav.css',
|
||||
'/static/haonav/js/nav.js',
|
||||
'/static/haonav/img/default.png'
|
||||
];
|
||||
|
||||
self.addEventListener('install', function (e) {
|
||||
e.waitUntil(
|
||||
caches.open(CACHE).then(function (c) { return c.addAll(PRECACHE); }).then(function () {
|
||||
return self.skipWaiting();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('activate', function (e) {
|
||||
e.waitUntil(
|
||||
caches.keys().then(function (keys) {
|
||||
return Promise.all(keys.filter(function (k) { return k !== CACHE; }).map(function (k) { return caches.delete(k); }));
|
||||
}).then(function () { return self.clients.claim(); })
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', function (e) {
|
||||
var req = e.request;
|
||||
if (req.method !== 'GET') { return; }
|
||||
var url = new URL(req.url);
|
||||
if (url.origin !== location.origin) { return; }
|
||||
|
||||
// 接口类请求不缓存(收藏 / 组件 / 巡检 / 搜索建议)
|
||||
if (/^\/haonav\/(favorite|widget|task|suggest)/.test(url.pathname)) { return; }
|
||||
|
||||
// 静态资源 & 图标/截图缓存:缓存优先,后台更新
|
||||
if (url.pathname.indexOf('/static/haonav/') === 0 ||
|
||||
/^\/haonav\/(favicon|snapshot)/.test(url.pathname)) {
|
||||
e.respondWith(
|
||||
caches.match(req).then(function (hit) {
|
||||
var fetching = fetch(req).then(function (resp) {
|
||||
if (resp && resp.status === 200) {
|
||||
var clone = resp.clone();
|
||||
caches.open(CACHE).then(function (c) { c.put(req, clone); });
|
||||
}
|
||||
return resp;
|
||||
}).catch(function () { return hit; });
|
||||
return hit || fetching;
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 页面:网络优先,离线回退缓存
|
||||
if (req.mode === 'navigate' || url.pathname.indexOf('/haonav/') === 0) {
|
||||
e.respondWith(
|
||||
fetch(req).then(function (resp) {
|
||||
if (resp && resp.status === 200) {
|
||||
var clone = resp.clone();
|
||||
caches.open(CACHE).then(function (c) { c.put(req, clone); });
|
||||
}
|
||||
return resp;
|
||||
}).catch(function () {
|
||||
return caches.match(req);
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user