mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: update service worker
This commit is contained in:
1 file changed
+5
-5
@@ -175,7 +175,7 @@ async function get(request: Request) {
|
|||||||
console.warn(source.toString(), ' Load fail ', error);
|
console.warn(source.toString(), ' Load fail ', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fetch(request);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformUrl(url: string) {
|
function transformUrl(url: string) {
|
||||||
@@ -185,7 +185,7 @@ function transformUrl(url: string) {
|
|||||||
return urlObject.toString();
|
return urlObject.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cached(request: Request, cacheKey: string, fetchFunc: () => Promise<Response>) {
|
async function cached(request: Request, cacheKey: string, fetchFunc: () => Promise<Response | null>) {
|
||||||
const url = transformUrl(request.url);
|
const url = transformUrl(request.url);
|
||||||
const urlObject = new URL(url);
|
const urlObject = new URL(url);
|
||||||
const isAsset = config.assets.some((i) => request.url.startsWith(i));
|
const isAsset = config.assets.some((i) => request.url.startsWith(i));
|
||||||
@@ -211,10 +211,10 @@ async function cached(request: Request, cacheKey: string, fetchFunc: () => Promi
|
|||||||
}
|
}
|
||||||
const [cache, response] = await Promise.all([
|
const [cache, response] = await Promise.all([
|
||||||
caches.open(cacheKey),
|
caches.open(cacheKey),
|
||||||
fetchFunc(),
|
fetchFunc().catch(() => null),
|
||||||
]);
|
]);
|
||||||
if (response.status === 206) return response; // partial response cannot be cached
|
if (response?.status === 206) return response; // partial response cannot be cached
|
||||||
if (response.ok) {
|
if (response?.ok) {
|
||||||
console.log(`Cached ${url}`);
|
console.log(`Cached ${url}`);
|
||||||
cache.put(url, response.clone());
|
cache.put(url, response.clone());
|
||||||
return response;
|
return response;
|
||||||
|
|||||||
Reference in new issue
Block a user