Skip to content

Commit

Permalink
Ignore cache-related errors when fetching chunks in the service worker
Browse files Browse the repository at this point in the history
Appearst to be the more common error when in Twitter's in-app browser
(the out-of-memory error from 89cb333
only happens when reloading). I'm guessing that SFSafariViewController
has a much smaller quota (or no quota at all?) and is not able to open
or write to the cache.

If we ignore errors then we're able to load successfully.
  • Loading branch information
mihaip committed Mar 19, 2023
1 parent 89cb333 commit 7730718
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/emulator/emulator-service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,26 @@ function handleIdleWait(event: FetchEvent) {
}

async function handleDiskCacheRequest(request: Request): Promise<Response> {
const cache = await caches.open(DISK_CACHE_NAME);
// Ignore cache-related errors, we don't want to block the fetch if the
// cache fails (appears to happen on iOS when running with more constrained
// quotas, e.g. in SFSafariViewController).
let cache: Cache | undefined;
try {
cache = await caches.open(DISK_CACHE_NAME);
} catch (e) {
console.error("Error opening cache:", e);
}

// Kick off (but don't wait for) a prefetch of the next chunk regardless
// of whether this is a cache hit (if prefetching is working, we should
// almost always end up with a cache hit, but we want to keep fetching
// subsequent chunks).
prefetchNextChunk(cache, request.url);

const match = await cache.match(request);
if (match) {
return match;
if (cache) {
prefetchNextChunk(cache, request.url);
const match = await cache.match(request);
if (match) {
return match;
}
}
async function postMessage(data: any) {
const clients = await self.clients.matchAll({type: "window"});
Expand All @@ -112,7 +121,11 @@ async function handleDiskCacheRequest(request: Request): Promise<Response> {

postMessage({type: "disk_chunk_fetch_start"});
const response = await fetch(request);
cache.put(request, response.clone());
try {
cache?.put(request, response.clone());
} catch (e) {
console.error("Ignoring cache error: ", e);
}
postMessage({type: "disk_chunk_fetch_end"});
return response;
}
Expand Down

0 comments on commit 7730718

Please sign in to comment.