From 295c0aa82e60cab1295d4fd2882888d2c8ba5432 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 12 Apr 2024 21:12:37 +0530 Subject: [PATCH] Splinter --- web/packages/next/cache.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/web/packages/next/cache.ts b/web/packages/next/cache.ts index 94cc05be43..5368653ae7 100644 --- a/web/packages/next/cache.ts +++ b/web/packages/next/cache.ts @@ -97,7 +97,21 @@ const openWebCache = async (name: CacheName) => { /** An implementation of {@link EnteCache} using OPFS */ const openOPFSCacheWeb = async (name: CacheName) => { + // While all major browsers support OPFS now, their implementations still + // have various quirks. However, we don't need to handle all possible cases + // and can just instead use the APIs and guarantees Chromium provides since + // this code will only run in our Electron app (which'll use Chromium as the + // renderer). + // + // So for our purpose, this can serve as the docs for what's available: + // https://web.dev/articles/origin-private-file-system const cache = await caches.open(name); + + const root = await navigator.storage.getDirectory(); + const _caches = await root.getDirectoryHandle("cache", { create: true }); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const _cache = await _caches.getDirectoryHandle(name, { create: true }); + return { match: (key: string) => { return cache.match(key); @@ -141,6 +155,14 @@ export async function cached( * * Meant for use during logout, to reset the state of the user's account. */ -export const clearCaches = async () => { +export const clearCaches = async () => + globalThis.electron ? clearOPFSCaches() : clearWebCaches(); + +export const clearWebCaches = async () => { await Promise.all(cacheNames.map((name) => caches.delete(name))); }; + +export const clearOPFSCaches = async () => { + const root = await navigator.storage.getDirectory(); + await root.removeEntry("cache", { recursive: true }); +};