Files
qihuo/static/sw.js
T
dekun 3ba3be6035 ui: 持仓监控卡片与关键位同宽,完善多端与 PWA
改用 split-grid 全宽布局;手机/平板/电脑断点适配;更新 manifest 与 Service Worker 支持安装为 App。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 12:50:58 +08:00

65 lines
2.1 KiB
JavaScript

var CACHE_VERSION = 'qihuo-v3';
var STATIC_CACHE = CACHE_VERSION + '-static';
var STATIC_ASSETS = [
'/static/css/tech.css',
'/static/css/responsive.css',
'/static/css/trade.css',
'/static/js/theme.js',
'/static/js/nav.js',
'/static/js/pwa.js',
'/static/js/symbol.js',
'/static/js/trade.js',
'/static/icons/icon-192.png',
'/static/icons/icon-512.png',
'/static/icons/icon.svg',
'/static/manifest.json',
'/login'
];
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(STATIC_CACHE).then(function (cache) {
return cache.addAll(STATIC_ASSETS).catch(function () { /* ignore partial */ });
}).then(function () { return self.skipWaiting(); })
);
});
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(keys.filter(function (k) {
return k.startsWith('qihuo-') && k !== STATIC_CACHE;
}).map(function (k) { return caches.delete(k); }));
}).then(function () { return self.clients.claim(); })
);
});
self.addEventListener('fetch', function (event) {
var req = event.request;
if (req.method !== 'GET') return;
var url = new URL(req.url);
if (url.origin !== self.location.origin) return;
if (url.pathname.indexOf('/static/') === 0) {
event.respondWith(
caches.match(req).then(function (cached) {
return cached || fetch(req).then(function (res) {
var copy = res.clone();
caches.open(STATIC_CACHE).then(function (cache) { cache.put(req, copy); });
return res;
});
})
);
return;
}
if (req.mode === 'navigate' || (req.headers.get('accept') || '').indexOf('text/html') !== -1) {
event.respondWith(
fetch(req).catch(function () {
return caches.match('/login');
})
);
}
});