mirror of
https://github.com/immich-app/immich.git
synced 2025-06-04 06:04:21 -04:00
Cache/return the app.html for all web entry points
This commit is contained in:
parent
d25b743e29
commit
537c0ba801
@ -36,6 +36,12 @@ sw.addEventListener('fetch', (event) => {
|
|||||||
event.respondWith(appResources(url, event));
|
event.respondWith(appResources(url, event));
|
||||||
} else if (/^\/api\/assets\/[a-f0-9-]+\/(original|thumbnail)/.test(url.pathname)) {
|
} else if (/^\/api\/assets\/[a-f0-9-]+\/(original|thumbnail)/.test(url.pathname)) {
|
||||||
event.respondWith(immichAsset(url));
|
event.respondWith(immichAsset(url));
|
||||||
|
} else if (
|
||||||
|
/^(\/(link|auth|admin|albums|archive|buy|explore|favorites|folders|maps|memory|partners|people|photos|places|search|share|shared-links|sharing|tags|trash|user-settings|utilities))(\/.*)?$/.test(
|
||||||
|
url.pathname,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
event.respondWith(ssr(new URL(event.request.url).origin));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -50,31 +56,49 @@ async function addFilesToCache() {
|
|||||||
await cache.addAll(APP_RESOURCES);
|
await cache.addAll(APP_RESOURCES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ssr(origin: string) {
|
||||||
|
const cache = await caches.open(CACHE);
|
||||||
|
const url = new URL('/', origin);
|
||||||
|
let response = useCache ? await cache.match(url) : undefined;
|
||||||
|
if (response) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
response = await fetch(url);
|
||||||
|
if (!(response instanceof Response)) {
|
||||||
|
throw new TypeError('invalid response from fetch');
|
||||||
|
}
|
||||||
|
if (response.status === 200) {
|
||||||
|
cache.put(url, response.clone());
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
async function immichAsset(url: URL) {
|
async function immichAsset(url: URL) {
|
||||||
const cache = await caches.open(CACHE);
|
const cache = await caches.open(CACHE);
|
||||||
let response = useCache ? await cache.match(url) : undefined;
|
let response = useCache ? await cache.match(url) : undefined;
|
||||||
if (!response) {
|
if (response) {
|
||||||
try {
|
return response;
|
||||||
const cancelToken = new AbortController();
|
}
|
||||||
const request = fetch(url, {
|
try {
|
||||||
signal: cancelToken.signal,
|
const cancelToken = new AbortController();
|
||||||
});
|
const request = fetch(url, {
|
||||||
pendingLoads.set(url.toString(), cancelToken);
|
signal: cancelToken.signal,
|
||||||
response = await request;
|
});
|
||||||
if (!(response instanceof Response)) {
|
pendingLoads.set(url.toString(), cancelToken);
|
||||||
throw new TypeError('invalid response from fetch');
|
response = await request;
|
||||||
}
|
if (!(response instanceof Response)) {
|
||||||
if (response.status === 200) {
|
throw new TypeError('invalid response from fetch');
|
||||||
cache.put(url, response.clone());
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
if ((error as any).name !== 'AbortError') {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
pendingLoads.delete(url.toString());
|
|
||||||
}
|
}
|
||||||
|
if (response.status === 200) {
|
||||||
|
cache.put(url, response.clone());
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
if ((error as any).name !== 'AbortError') {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
pendingLoads.delete(url.toString());
|
||||||
}
|
}
|
||||||
return response as Response;
|
return response as Response;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user