diff --git a/client/src/db/cacheAccountSettings.ts b/client/src/db/cacheAccountSettings.ts index 3491094fc..521c76b8a 100644 --- a/client/src/db/cacheAccountSettings.ts +++ b/client/src/db/cacheAccountSettings.ts @@ -111,6 +111,18 @@ export async function cacheAccountSettings(settings: Partial): Promise } } +/** Refresh a partial write's result without changing the live settings. */ +export async function refreshCachedAccountSettings(load: () => Promise>): Promise { + if (!isAccountDb()) return; + const account = offlineDb.name; + try { + const settings = await load(); + if (account === offlineDb.name) await cacheAccountSettings(settings); + } catch { + // Keep the last confirmed settings until the next successful read. + } +} + /** * Lays settings the server just accepted over the kept answer, so a change made * online is what the next start without a connection shows. diff --git a/client/src/repo/atlasRepo.data.test.ts b/client/src/repo/atlasRepo.data.test.ts index fdfe1bb81..8a5520416 100644 --- a/client/src/repo/atlasRepo.data.test.ts +++ b/client/src/repo/atlasRepo.data.test.ts @@ -187,7 +187,7 @@ describe('online writes', () => { await atlasRepo.unmarkCountry('IS'); expect(hits).toEqual(['region', 'country']); - expect((await keptAtlasPart('regions'))?.data).toEqual({ ES: [] }); + expect((await keptAtlasPart('regions'))?.data).toEqual({}); expect((await keptAtlasPart('stats'))?.data).toMatchObject({ countries: [], stats: { totalCountries: 0 } }); expect(await offlineDb.userMutationQueue.count()).toBe(0); }); diff --git a/client/src/repo/atlasRepo.ts b/client/src/repo/atlasRepo.ts index 9db98cadf..49b0b0b11 100644 --- a/client/src/repo/atlasRepo.ts +++ b/client/src/repo/atlasRepo.ts @@ -86,7 +86,11 @@ async function readThrough(part: AtlasSnapshotPart, fetch: () => Promise): const account = offlineDb.name; const data = await fetch(); if (account !== offlineDb.name) return data; - await keepAtlasPart(part, data).catch(() => undefined); + try { + await keepAtlasPart(part, data); + } catch { + // A full or disabled cache still allows an online read. + } if (account !== offlineDb.name) return data; return (await withWaitingWrites(part, data).catch(() => data)) as T; }, @@ -127,7 +131,11 @@ async function write(spec: WriteSpec): Promise { headers: { 'X-Idempotency-Key': id }, }); if (account === offlineDb.name) { - await reconcileAtlasWrite({ ...spec, body }).catch(() => undefined); + try { + await reconcileAtlasWrite({ ...spec, body }); + } catch { + // The server accepted the write. A later online read can repair the cached copy. + } } return; } catch (err) { @@ -240,9 +248,13 @@ export const atlasRepo = { const account = offlineDb.name; const item = (await apiClient.post('/addons/atlas/bucket-list', data)).data?.item as unknown; if (account !== offlineDb.name) return item; - const kept = await keptAtlasPart('bucketList').catch(() => undefined); - if (account === offlineDb.name && kept && Array.isArray(kept.data) && item) { - await keepAtlasPart('bucketList', [item, ...kept.data]).catch(() => undefined); + try { + const kept = await keptAtlasPart('bucketList'); + if (account === offlineDb.name && kept && Array.isArray(kept.data) && item) { + await keepAtlasPart('bucketList', [item, ...kept.data]); + } + } catch { + // The wish already exists on the server. An online refresh can restore its offline copy. } return item; }, diff --git a/client/src/store/settingsStore.offline.test.ts b/client/src/store/settingsStore.offline.test.ts index 0cb2c4a25..d2cdee533 100644 --- a/client/src/store/settingsStore.offline.test.ts +++ b/client/src/store/settingsStore.offline.test.ts @@ -159,5 +159,6 @@ describe('settings without a connection', () => { await vi.waitFor(async () => expect(await cachedAccountSettings()).toEqual({ map_provider: 'leaflet', distance_unit: 'imperial' }) ); + expect(useSettingsStore.getState().settings.map_provider).toBe('mapbox-gl'); }); }); diff --git a/client/src/store/settingsStore.ts b/client/src/store/settingsStore.ts index 538f9999f..69a68dbcf 100644 --- a/client/src/store/settingsStore.ts +++ b/client/src/store/settingsStore.ts @@ -13,7 +13,12 @@ import { SUPPORTED_LANGUAGE_CODES } from '../i18n/supportedLanguages' import { detectBrowserLanguage } from '../i18n/detectBrowserLanguage' import { normalizeTileUrl, stripTileApiKey } from '../utils/tileUrl' import { clearTileCache } from '../sync/tilePrefetcher' -import { cacheAccountSettings, cacheAccountSettingsPatch, cachedAccountSettings } from '../db/cacheAccountSettings' +import { + cacheAccountSettings, + cacheAccountSettingsPatch, + cachedAccountSettings, + refreshCachedAccountSettings, +} from '../db/cacheAccountSettings' import { rememberStartDestination, DEFAULT_START_PAGE, DEFAULT_START_TRIP_TAB } from '../utils/startDestination' import { clearGlEngineFailure } from '../utils/webgl' @@ -255,12 +260,15 @@ export const useSettingsStore = create((set, get) => ({ rememberStartDestination(patch) try { const saved: SettingsBulkResponse | undefined = await settingsApi.setBulk(patch) - if (!saved) return + if (typeof saved?.updated !== 'number') return const managed = new Set(saved.managed_keys) const accepted = Object.entries(patch).filter(([key]) => !managed.has(key)) // Some no-ops report only a count, so fetch what was actually saved. if (accepted.length === saved.updated) void cacheAccountSettingsPatch(Object.fromEntries(accepted)) - else void get().loadSettings() + else void refreshCachedAccountSettings(async () => { + const data = await settingsApi.get() + return withNormalizedTileUrl({ ...data.settings } as Partial) + }) } catch (err: unknown) { console.error('Failed to save settings:', err) throw new Error(getApiErrorMessage(err, 'Error saving settings')) diff --git a/client/src/sync/glTileCache.ts b/client/src/sync/glTileCache.ts index 6ab0bc60f..d22ec4a00 100644 --- a/client/src/sync/glTileCache.ts +++ b/client/src/sync/glTileCache.ts @@ -42,7 +42,7 @@ export function isGlRasterTileRequest({ url, request }: { url: URL; request: Pic /** Whether prefetch and the service worker can keep readable GL tiles for this template. */ export function hasGlRasterCache(template: string): boolean { try { - const url = new URL(template.replaceAll('{s}', 'a')); + const url = new URL(template.replace(/\{s\}/g, 'a')); return isGlRasterTileRequest({ url, request: { mode: 'cors' } }); } catch { return false;