From ed6724035e67c47382dc84b9b9c27e5870d82d00 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sun, 8 Nov 2026 12:26:07 +0100 Subject: [PATCH] fix(offline): a Cache Storage open that never settles is given up on, so preparing for offline and the offline map no longer wait on it forever --- client/src/native/offlineTiles.ts | 10 +++--- client/src/sync/glPrefetcher.ts | 11 +++--- client/src/sync/openCacheStorage.test.ts | 37 ++++++++++++++++++++ client/src/sync/openCacheStorage.ts | 43 ++++++++++++++++++++++++ client/src/sync/tilePrefetcher.ts | 8 ++--- 5 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 client/src/sync/openCacheStorage.test.ts create mode 100644 client/src/sync/openCacheStorage.ts diff --git a/client/src/native/offlineTiles.ts b/client/src/native/offlineTiles.ts index 2806617b2..676c4e8fb 100644 --- a/client/src/native/offlineTiles.ts +++ b/client/src/native/offlineTiles.ts @@ -1,5 +1,6 @@ import type { AddProtocolAction, GetResourceResponse, RequestParameters } from 'maplibre-gl'; import { VECTOR_CACHE } from '../sync/glPrefetcher'; +import { openCacheStorage } from '../sync/openCacheStorage'; import { glMapsThroughAppSchemes } from './glSchemes'; import { isNativeIos } from './platform'; @@ -110,12 +111,9 @@ async function load(url: string, networkFirst: boolean, signal: AbortSignal): Pr * network: the store is what keeps it offline, not what lets it draw at all. */ async function openCache(): Promise { - try { - return await caches.open(VECTOR_CACHE); - } catch { - // Refused by the web view: read from the network instead, which is all a cache miss does anyway. - return null; - } + // Refused, or left pending, by the web view: read from the network instead, + // which is all a cache miss does anyway. + return openCacheStorage(VECTOR_CACHE); } export const offlineProtocol: AddProtocolAction = async ( diff --git a/client/src/sync/glPrefetcher.ts b/client/src/sync/glPrefetcher.ts index 27b7aeb87..9257f5fbe 100644 --- a/client/src/sync/glPrefetcher.ts +++ b/client/src/sync/glPrefetcher.ts @@ -17,6 +17,8 @@ */ import { computeBbox, tileRange, type TileBbox } from './tilePrefetcher' import type { Place } from '../types' +import { openCacheStorage } from './openCacheStorage' +import { isNativeIos } from '../native/platform' /** OpenFreeMap serves vector tiles up to z14 and overzooms from there. */ const VECTOR_MAX_ZOOM = 14 @@ -156,11 +158,7 @@ async function warm(cache: Cache | null, url: string): Promise { } async function openVectorCache(): Promise { - try { - return await caches.open(VECTOR_CACHE) - } catch { - return null - } + return openCacheStorage(VECTOR_CACHE) } /** @@ -277,6 +275,9 @@ export async function prefetchVectorForPlaces( isCancelled: () => boolean = () => false, ): Promise { if (!navigator.onLine) return { tiles: 0, template: null } + // The iOS app has no service worker: with Cache Storage given up on, nothing + // would keep what is downloaded, so nothing is downloaded. + if (isNativeIos() && !(await openVectorCache())) return { tiles: 0, template: null } const bbox = computeBbox(places) if (!bbox) return { tiles: 0, template: null } diff --git a/client/src/sync/openCacheStorage.test.ts b/client/src/sync/openCacheStorage.test.ts new file mode 100644 index 000000000..4086290b1 --- /dev/null +++ b/client/src/sync/openCacheStorage.test.ts @@ -0,0 +1,37 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { _resetCacheStorage, cacheStorageUnusable, openCacheStorage } from './openCacheStorage'; + +beforeEach(() => _resetCacheStorage()); +afterEach(() => { + vi.useRealTimers(); + vi.unstubAllGlobals(); +}); + +describe('openCacheStorage', () => { + it('FE-SYNC-CACHEOPEN-001: hands back the bucket, and null where Cache Storage is missing or throws', async () => { + const bucket = {} as Cache; + vi.stubGlobal('caches', { open: vi.fn(async () => bucket) }); + expect(await openCacheStorage('tiles')).toBe(bucket); + + vi.stubGlobal('caches', { open: vi.fn(async () => Promise.reject(new DOMException('denied', 'SecurityError'))) }); + expect(await openCacheStorage('tiles')).toBeNull(); + expect(cacheStorageUnusable()).toBe(false); + + vi.stubGlobal('caches', undefined); + expect(await openCacheStorage('tiles')).toBeNull(); + }); + + it('FE-SYNC-CACHEOPEN-002: an open that never settles is given up on after a few seconds, and not tried again', async () => { + vi.useFakeTimers(); + const open = vi.fn(() => new Promise(() => undefined)); + vi.stubGlobal('caches', { open }); + + const pending = openCacheStorage('gl-map-offline'); + await vi.advanceTimersByTimeAsync(4000); + expect(await pending).toBeNull(); + expect(cacheStorageUnusable()).toBe(true); + + expect(await openCacheStorage('gl-map-offline')).toBeNull(); + expect(open).toHaveBeenCalledTimes(1); + }); +}); diff --git a/client/src/sync/openCacheStorage.ts b/client/src/sync/openCacheStorage.ts new file mode 100644 index 000000000..23bd0b296 --- /dev/null +++ b/client/src/sync/openCacheStorage.ts @@ -0,0 +1,43 @@ +/** + * Opens a Cache Storage bucket, or null where it cannot be had: no Cache + * Storage at all, an open that throws, or one that never settles. + * + * The last is the one that hurt. On iPadOS the app's web view can leave + * caches.open() pending for good instead of refusing it, and everything that + * awaited it waited with it: "prepare for offline" stood still on its first + * trip's map tiles, and the offline map protocol never answered MapLibre, so + * the map stayed blank without an error. An open that has not settled after a + * few seconds is given up on, and Cache Storage is not tried again for the + * rest of the session, so no later caller pays the wait again. + */ + +const OPEN_TIMEOUT_MS = 4000; + +let unusable = false; + +export async function openCacheStorage(name: string): Promise { + if (unusable || typeof caches === 'undefined') return null; + let timer: ReturnType | undefined; + const gaveUp = new Promise((resolve) => { + timer = setTimeout(() => resolve(null), OPEN_TIMEOUT_MS); + }); + try { + const cache = await Promise.race([caches.open(name), gaveUp]); + if (!cache) unusable = true; + return cache; + } catch { + return null; + } finally { + clearTimeout(timer); + } +} + +/** Whether Cache Storage was given up on this session. */ +export function cacheStorageUnusable(): boolean { + return unusable; +} + +/** Tests only: try Cache Storage again. */ +export function _resetCacheStorage(): void { + unusable = false; +} diff --git a/client/src/sync/tilePrefetcher.ts b/client/src/sync/tilePrefetcher.ts index 8e153bf2e..226be9b06 100644 --- a/client/src/sync/tilePrefetcher.ts +++ b/client/src/sync/tilePrefetcher.ts @@ -31,6 +31,7 @@ import { isOfflineForbiddenTemplate, isVectorStyle, normalizeTileUrl, resolveTil import { OFM_DARK, OFM_POSITRON } from '../constants/mapDefaults' import { clearVectorCache, prefetchStyleAssets, prefetchVectorForPlaces } from './glPrefetcher' import { isNativeIos } from '../native/platform' +import { openCacheStorage } from './openCacheStorage' // ── Constants ───────────────────────────────────────────────────────────────── @@ -275,12 +276,7 @@ function enumerateTiles( /** Open the tile cache, or null where Cache Storage isn't available. */ async function openTileCache(): Promise { - try { - if (typeof caches === 'undefined') return null - return await caches.open(TILE_CACHE) - } catch { - return null - } + return openCacheStorage(TILE_CACHE) } /**