mirror of
https://github.com/immich-app/immich.git
synced 2025-07-09 03:04:16 -04:00
revert: service worker changes (#19227)
This commit is contained in:
parent
35280b94cc
commit
91cbd56c1c
@ -1,4 +1,4 @@
|
|||||||
import { cancelLoad, getCachedOrFetch } from './cache';
|
import { cancelLoad, getCachedOrFetch } from './fetch-event';
|
||||||
|
|
||||||
export const installBroadcastChannelListener = () => {
|
export const installBroadcastChannelListener = () => {
|
||||||
const broadcast = new BroadcastChannel('immich');
|
const broadcast = new BroadcastChannel('immich');
|
||||||
|
@ -1,106 +0,0 @@
|
|||||||
import { build, files, version } from '$service-worker';
|
|
||||||
|
|
||||||
const useCache = true;
|
|
||||||
const CACHE = `cache-${version}`;
|
|
||||||
|
|
||||||
export const APP_RESOURCES = [
|
|
||||||
...build, // the app itself
|
|
||||||
...files, // everything in `static`
|
|
||||||
];
|
|
||||||
|
|
||||||
export const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined;
|
|
||||||
export const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined;
|
|
||||||
|
|
||||||
export async function deleteOldCaches() {
|
|
||||||
for (const key of await caches.keys()) {
|
|
||||||
if (key !== CACHE) {
|
|
||||||
await caches.delete(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function addFilesToCache() {
|
|
||||||
const cache = await caches.open(CACHE);
|
|
||||||
await cache.addAll(APP_RESOURCES);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pendingLoads = new Map<string, AbortController>();
|
|
||||||
|
|
||||||
export async function cancelLoad(urlString: string) {
|
|
||||||
const pending = pendingLoads.get(urlString);
|
|
||||||
if (pending) {
|
|
||||||
pending.abort();
|
|
||||||
pendingLoads.delete(urlString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getCachedOrFetch(request: URL | Request | string, cancelable: boolean = false) {
|
|
||||||
const cached = await checkCache(request);
|
|
||||||
if (cached.response) {
|
|
||||||
return cached.response;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (!cancelable) {
|
|
||||||
const response = await fetch(request);
|
|
||||||
checkResponse(response);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await fetchWithCancellation(request, cached.cache);
|
|
||||||
} catch {
|
|
||||||
return new Response(undefined, {
|
|
||||||
status: 499,
|
|
||||||
statusText: 'Request canceled: Instructions unclear, accidentally interrupted myself',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchWithCancellation(request: URL | Request | string, cache: Cache) {
|
|
||||||
const cacheKey = getCacheKey(request);
|
|
||||||
const cancelToken = new AbortController();
|
|
||||||
|
|
||||||
try {
|
|
||||||
pendingLoads.set(cacheKey, cancelToken);
|
|
||||||
const response = await fetch(request, {
|
|
||||||
signal: cancelToken.signal,
|
|
||||||
});
|
|
||||||
|
|
||||||
checkResponse(response);
|
|
||||||
setCached(response, cache, cacheKey);
|
|
||||||
return response;
|
|
||||||
} finally {
|
|
||||||
pendingLoads.delete(cacheKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function checkCache(url: URL | Request | string) {
|
|
||||||
if (!useCache) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const cache = await caches.open(CACHE);
|
|
||||||
const response = await cache.match(url);
|
|
||||||
return { cache, response };
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setCached(response: Response, cache: Cache, cacheKey: URL | Request | string) {
|
|
||||||
if (response.status === 200) {
|
|
||||||
cache.put(cacheKey, response.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkResponse(response: Response) {
|
|
||||||
if (!(response instanceof Response)) {
|
|
||||||
throw new TypeError('invalid response from fetch');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCacheKey(request: URL | Request | string) {
|
|
||||||
if (isURL(request)) {
|
|
||||||
return request.toString();
|
|
||||||
} else if (isRequest(request)) {
|
|
||||||
return request.url;
|
|
||||||
} else {
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +1,111 @@
|
|||||||
import { APP_RESOURCES, getCachedOrFetch } from './cache';
|
import { version } from '$service-worker';
|
||||||
|
|
||||||
|
const useCache = true;
|
||||||
|
const CACHE = `cache-${version}`;
|
||||||
|
|
||||||
|
export const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined;
|
||||||
|
export const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined;
|
||||||
|
|
||||||
|
export async function deleteOldCaches() {
|
||||||
|
for (const key of await caches.keys()) {
|
||||||
|
if (key !== CACHE) {
|
||||||
|
await caches.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pendingLoads = new Map<string, AbortController>();
|
||||||
|
|
||||||
|
export async function cancelLoad(urlString: string) {
|
||||||
|
const pending = pendingLoads.get(urlString);
|
||||||
|
if (pending) {
|
||||||
|
pending.abort();
|
||||||
|
pendingLoads.delete(urlString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getCachedOrFetch(request: URL | Request | string, cancelable: boolean = false) {
|
||||||
|
const cached = await checkCache(request);
|
||||||
|
if (cached.response) {
|
||||||
|
return cached.response;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!cancelable) {
|
||||||
|
const response = await fetch(request);
|
||||||
|
checkResponse(response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await fetchWithCancellation(request, cached.cache);
|
||||||
|
} catch {
|
||||||
|
return new Response(undefined, {
|
||||||
|
status: 499,
|
||||||
|
statusText: 'Request canceled: Instructions unclear, accidentally interrupted myself',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchWithCancellation(request: URL | Request | string, cache: Cache) {
|
||||||
|
const cacheKey = getCacheKey(request);
|
||||||
|
const cancelToken = new AbortController();
|
||||||
|
|
||||||
|
try {
|
||||||
|
pendingLoads.set(cacheKey, cancelToken);
|
||||||
|
const response = await fetch(request, {
|
||||||
|
signal: cancelToken.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
checkResponse(response);
|
||||||
|
setCached(response, cache, cacheKey);
|
||||||
|
return response;
|
||||||
|
} finally {
|
||||||
|
pendingLoads.delete(cacheKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkCache(url: URL | Request | string) {
|
||||||
|
if (!useCache) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const cache = await caches.open(CACHE);
|
||||||
|
const response = await cache.match(url);
|
||||||
|
return { cache, response };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setCached(response: Response, cache: Cache, cacheKey: URL | Request | string) {
|
||||||
|
if (response.status === 200) {
|
||||||
|
cache.put(cacheKey, response.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkResponse(response: Response) {
|
||||||
|
if (!(response instanceof Response)) {
|
||||||
|
throw new TypeError('invalid response from fetch');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCacheKey(request: URL | Request | string) {
|
||||||
|
if (isURL(request)) {
|
||||||
|
return request.toString();
|
||||||
|
} else if (isRequest(request)) {
|
||||||
|
return request.url;
|
||||||
|
} else {
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function isAssetRequest(pathname: string): boolean {
|
function isAssetRequest(pathname: string): boolean {
|
||||||
return /^\/api\/assets\/[a-f0-9-]+\/(original|thumbnail)/.test(pathname);
|
return /^\/api\/assets\/[a-f0-9-]+\/(original|thumbnail)/.test(pathname);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isIgnoredFileType(pathname: string): boolean {
|
|
||||||
return /\.(png|ico|txt|json|ts|ttf|css|js|svelte)$/.test(pathname);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isIgnoredPath(pathname: string): boolean {
|
|
||||||
return /^\/(src|api)(\/.*)?$/.test(pathname) || /^\/(node_modules|@vite|@id)(\/.*)?$/.test(pathname);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function handleFetchEvent(event: FetchEvent): void {
|
export function handleFetchEvent(event: FetchEvent): void {
|
||||||
if (event.request.method !== 'GET') {
|
if (event.request.method !== 'GET') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(event.request.url);
|
const url = new URL(event.request.url);
|
||||||
|
if (url.origin !== self.location.origin) {
|
||||||
if (APP_RESOURCES.includes(url.pathname)) {
|
|
||||||
event.respondWith(getCachedOrFetch(event.request));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,11 +113,4 @@ export function handleFetchEvent(event: FetchEvent): void {
|
|||||||
event.respondWith(getCachedOrFetch(event.request, true));
|
event.respondWith(getCachedOrFetch(event.request, true));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isIgnoredFileType(url.pathname) || isIgnoredPath(url.pathname)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const slash = new URL('/', url.origin);
|
|
||||||
event.respondWith(getCachedOrFetch(slash));
|
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,17 @@
|
|||||||
/// <reference lib="esnext" />
|
/// <reference lib="esnext" />
|
||||||
/// <reference lib="webworker" />
|
/// <reference lib="webworker" />
|
||||||
import { installBroadcastChannelListener } from './broadcast-channel';
|
import { installBroadcastChannelListener } from './broadcast-channel';
|
||||||
import { addFilesToCache, deleteOldCaches } from './cache';
|
import { deleteOldCaches, handleFetchEvent } from './fetch-event';
|
||||||
import { handleFetchEvent } from './fetch-event';
|
|
||||||
|
|
||||||
const sw = globalThis as unknown as ServiceWorkerGlobalScope;
|
const sw = globalThis as unknown as ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
const handleActivate = (event: ExtendableEvent) => {
|
const handleActivate = (event: ExtendableEvent) => {
|
||||||
event.waitUntil(sw.clients.claim());
|
event.waitUntil(sw.clients.claim());
|
||||||
// Remove previous cached data from disk
|
|
||||||
event.waitUntil(deleteOldCaches());
|
event.waitUntil(deleteOldCaches());
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInstall = (event: ExtendableEvent) => {
|
const handleInstall = (event: ExtendableEvent) => {
|
||||||
event.waitUntil(sw.skipWaiting());
|
event.waitUntil(sw.skipWaiting());
|
||||||
// Create a new cache and add all files to it
|
|
||||||
event.waitUntil(addFilesToCache());
|
|
||||||
};
|
};
|
||||||
|
|
||||||
sw.addEventListener('install', handleInstall, { passive: true });
|
sw.addEventListener('install', handleInstall, { passive: true });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user