Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fdm-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 46 additions & 16 deletions fdm-app/app/routes/farm.$b_id_farm.$calendar.atlas.elevation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion fdm-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@svenvw/fdm-app",
"version": "0.26.1",
"version": "0.26.2",
"private": true,
"sideEffects": false,
"type": "module",
Expand Down