三端自适应布局与 PWA 可安装支持
新增响应式样式、手机侧滑导航、manifest 与 Service Worker;补充根路径重定向与安装 App 入口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
var CACHE_VERSION = 'qihuo-v2';
|
||||
var STATIC_CACHE = CACHE_VERSION + '-static';
|
||||
var STATIC_ASSETS = [
|
||||
'/static/css/tech.css',
|
||||
'/static/css/responsive.css',
|
||||
'/static/js/theme.js',
|
||||
'/static/js/nav.js',
|
||||
'/static/js/pwa.js',
|
||||
'/static/js/symbol.js',
|
||||
'/static/icons/icon-192.png',
|
||||
'/static/icons/icon-512.png',
|
||||
'/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');
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user