mirror of
https://github.com/levywang/avhub.git
synced 2026-02-21 08:47:22 +08:00
74 lines
1.9 KiB
JavaScript
74 lines
1.9 KiB
JavaScript
const CACHE_NAME = 'avhub-cache-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/style.css',
|
|
'/config.js',
|
|
'/script.js',
|
|
'/globals.js',
|
|
'/manifest.json',
|
|
'/imgs/favicon.ico',
|
|
'/imgs/icon-192x192.png',
|
|
'/imgs/icon-512x512.png',
|
|
'https://testingcf.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css',
|
|
'https://testingcf.jsdelivr.net/npm/hls.js@1.4.12/dist/hls.min.js'
|
|
];
|
|
|
|
// 安装 Service Worker
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.addAll(urlsToCache))
|
|
);
|
|
});
|
|
|
|
// 激活 Service Worker
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
// 处理请求
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// 如果在缓存中找到响应,则返回缓存的响应
|
|
if (response) {
|
|
return response;
|
|
}
|
|
|
|
// 克隆请求,因为请求是一个流,只能使用一次
|
|
const fetchRequest = event.request.clone();
|
|
|
|
// 发起网络请求
|
|
return fetch(fetchRequest).then(response => {
|
|
// 检查是否收到有效的响应
|
|
if (!response || response.status !== 200 || response.type !== 'basic') {
|
|
return response;
|
|
}
|
|
|
|
// 克隆响应,因为响应是一个流,只能使用一次
|
|
const responseToCache = response.clone();
|
|
|
|
// 将响应添加到缓存
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
cache.put(event.request, responseToCache);
|
|
});
|
|
|
|
return response;
|
|
});
|
|
})
|
|
);
|
|
});
|