From 2a30b7be792ca48b552a134da600c31b177466f2 Mon Sep 17 00:00:00 2001 From: Sven Verweij <37927107+SvenVw@users.noreply.github.com> Date: Tue, 23 Dec 2025 09:57:12 +0100 Subject: [PATCH 1/2] fix: mobile elevation map stability by correcting the cache endpoint path, implementing robust localStorage error handling, and adding a 'fallback-to-stale' strategy for offline resilience --- .changeset/fix-mobile-elevation-fallback.md | 5 ++ ...m.$b_id_farm.$calendar.atlas.elevation.tsx | 62 ++++++++++++++----- 2 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 .changeset/fix-mobile-elevation-fallback.md diff --git a/.changeset/fix-mobile-elevation-fallback.md b/.changeset/fix-mobile-elevation-fallback.md new file mode 100644 index 000000000..ec2c10c7f --- /dev/null +++ b/.changeset/fix-mobile-elevation-fallback.md @@ -0,0 +1,5 @@ +--- +"@svenvw/fdm-app": patch +--- + +Fixed mobile elevation map stability by correcting the cache endpoint path, implementing robust `localStorage` error handling, and adding a "fallback-to-stale" strategy for offline resilience. diff --git a/fdm-app/app/routes/farm.$b_id_farm.$calendar.atlas.elevation.tsx b/fdm-app/app/routes/farm.$b_id_farm.$calendar.atlas.elevation.tsx index bd0d7a3ad..7c8939489 100644 --- a/fdm-app/app/routes/farm.$b_id_farm.$calendar.atlas.elevation.tsx +++ b/fdm-app/app/routes/farm.$b_id_farm.$calendar.atlas.elevation.tsx @@ -221,33 +221,44 @@ export default function FarmAtlasElevationBlock() { async function fetchIndex() { const cacheKey = "ahn_kaartbladindex_v1" setNetworkStatus("loading") + + // Try cache first try { - // Try cache if (typeof localStorage !== "undefined") { const cached = localStorage.getItem(cacheKey) if (cached) { - try { - const { timestamp, data } = JSON.parse(cached) - // Cache for 7 days - if ( - Date.now() - timestamp < - 7 * 24 * 60 * 60 * 1000 - ) { - setIndexData(data) - setNetworkStatus("idle") - return - } - } catch { - localStorage.removeItem(cacheKey) + const { timestamp, data } = JSON.parse(cached) + // Cache for 24 hours and ensure data is valid + if ( + data?.features?.length > 0 && + Date.now() - timestamp < + 24 * 60 * 60 * 1000 + ) { + setIndexData(data) + setNetworkStatus("idle") + return } } } + } catch (e) { + console.warn("Cache lookup failed, proceeding to fetch:", e) + try { + if (typeof localStorage !== "undefined") { + localStorage.removeItem(cacheKey) + } + } catch {} + } - // Fetch from our server-side cache - const response = await fetch("/resources/ahn-index") + // Fetch from our server-side cache + try { + const response = await fetch("/atlas/ahn-index") if (!response.ok) throw new Error("Failed to fetch COG index") const data = (await response.json()) as FeatureCollection + if (!data.features || data.features.length === 0) { + throw new Error("Empty AHN index received") + } + setIndexData(data) setNetworkStatus("idle") @@ -263,6 +274,25 @@ export default function FarmAtlasElevationBlock() { } } catch (e) { console.error("Error fetching COG index:", e) + + // Final fallback: Use expired cache if network failed but we have something + if (typeof localStorage !== "undefined") { + try { + const cached = localStorage.getItem(cacheKey) + if (cached) { + const { data } = JSON.parse(cached) + if (data?.features?.length > 0) { + console.warn( + "Network failed, using expired cache as fallback", + ) + setIndexData(data) + setNetworkStatus("idle") + return + } + } + } catch {} + } + setNetworkStatus("error") } } From dfbefd65dec4fa11b94c913d7f414b8dfb87dff4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 23 Dec 2025 09:07:44 +0000 Subject: [PATCH 2/2] chore: bump version of packages for release --- .changeset/fix-mobile-elevation-fallback.md | 5 ----- fdm-app/CHANGELOG.md | 6 ++++++ fdm-app/package.json | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 .changeset/fix-mobile-elevation-fallback.md diff --git a/.changeset/fix-mobile-elevation-fallback.md b/.changeset/fix-mobile-elevation-fallback.md deleted file mode 100644 index ec2c10c7f..000000000 --- a/.changeset/fix-mobile-elevation-fallback.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@svenvw/fdm-app": patch ---- - -Fixed mobile elevation map stability by correcting the cache endpoint path, implementing robust `localStorage` error handling, and adding a "fallback-to-stale" strategy for offline resilience. diff --git a/fdm-app/CHANGELOG.md b/fdm-app/CHANGELOG.md index 9dc444af5..15c197d2e 100644 --- a/fdm-app/CHANGELOG.md +++ b/fdm-app/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog fdm-app +## 0.26.2 + +### Patch Changes + +- 2a30b7b: Fixed mobile elevation map stability by correcting the cache endpoint path, implementing robust `localStorage` error handling, and adding a "fallback-to-stale" strategy for offline resilience. + ## 0.26.1 ### Patch Changes diff --git a/fdm-app/package.json b/fdm-app/package.json index 0f4c448c5..acef13227 100644 --- a/fdm-app/package.json +++ b/fdm-app/package.json @@ -1,6 +1,6 @@ { "name": "@svenvw/fdm-app", - "version": "0.26.1", + "version": "0.26.2", "private": true, "sideEffects": false, "type": "module",