
New Dogwalk story – Offline walks, geofence and sunlight contrast
The walker is on the street, phone in hand, dog pulling the leash. The 4G signal drops halfway down the block. Before, that meant a lost event: the pee, the poop, the photo, the GPS point — they simply never reached the server. The walk continued, but the record had a hole in it.
That was the problem commit 620b063 solved. The change was not small: 12 files, 2187 insertions and 143 deletions, all concentrated on one central idea — the walk cannot depend on the network.
The tools I use on this project live in the skills store — the first one is free.
What happened
1. Offline event queue (useWalkOfflineSync)
A hook was created that keeps a persistent queue in localStorage under the key patapass_walk_offline_queue. Each event (pee, poop, photo, GPS, finish) becomes an object with a UUID, type, coordinates, walk ID, timestamp and retry counter.
- Automatic sync every 10 seconds while online (
SYNC_INTERVAL_MS = 10_000). - 2-second debounce after reconnection (
SYNC_DEBOUNCE_MS = 2_000). - Maximum of 3 attempts per event (
MAX_RETRIES = 3); after that the event is marked as failed. - FIFO draining: the first to fail is the first to retry.
- The queue survives crashes and browser closure because it lives in
localStorage.
ActiveWalkInterface now uses this hook. When the walker registers an event, the UI responds immediately (adds to local state) and tries to send it to the server. If the request fails, the event is enqueued automatically via enqueueEvent.
2. Connection monitoring (useNetworkStatus)
Instead of trusting navigator.onLine (which lies), the app now pings the /health endpoint every 15 seconds (PING_INTERVAL_MS = 15_000), with a 5-second timeout per attempt. Quality is classified into four levels:
| Level | Max latency | Color |
|---|---|---|
| Excellent | 100 ms | green |
| Good | 300 ms | light green |
| Unstable | 800 ms | yellow |
| Weak | above 800 ms | red |
When the connection drops, retry uses exponential backoff: 5s, 15s, 45s, up to a 2-minute cap (RETRY_MAX_MS = 120_000, multiplier 3).
3. Status banner (WalkOfflineBanner)
A yellow bar appears at the top of the active walk with three possible states: offline, syncing and error. The walker sees how many events are pending and whether syncing is in progress.
4. Service-area geofence (professional.js)
The walker defines a service area (center + radius in km). During the walk, the app checks whether the current position is inside that area. The strategy has two levels:
- Try the dedicated endpoint
/api/service-areas/check-geofence(POST) with authentication. - If the endpoint fails, fall back client-side: fetch the walker’s active areas from the database and compute the distance with the Haversine formula. If the distance is less than or equal to the configured radius, the walker is inside the area.
Eight modules of professional.js were migrated from supabase.rpc to dbApi, removing direct Supabase references from the refactored components.
5. Area circle on the map (generateCircle in mapUtils.js)
To render the service area on the Leaflet map, the generateCircle(lat, lng, radiusKm, segments = 32) function was created. It generates a GeoJSON Polygon with 32 segments, computing latitude and longitude steps based on the center’s latitude (because one degree of longitude is worth less near the poles). The ServiceAreaCircle component renders this polygon over the map.
6. High contrast for use under the sun
Seven legibility points were fixed to reach WCAG AA under direct sunlight. Among them: statLabel, eventBtn, finishBtn, panel borders and text opacities. Card borders went from 1px solid #f5a62322 to 1.5px solid #f0d48a, and panel shadows from rgba(245, 166, 35, 0.1) to 0.18. Small opacity changes that make a difference when the sun is hitting the screen.
How it was solved
Migrating from direct supabase calls to dbApi was the prerequisite. With the API layer isolated, it was possible to inject the offline fallback without touching every individual call. ActiveWalkInterface swapped import { supabase } for import { db } and started using enqueueEvent when a request fails.
The offline queue was designed to be simple: localStorage, JSON, FIFO. No IndexedDB or Service Worker. The simplicity was a deliberate choice — the walker can close the app, reopen it hours later, and the queue is still there waiting to sync.
For the geofence, the client-side Haversine fallback ensures verification works even when the dedicated endpoint is down. The formula computes the straight-line distance between the GPS position and the center of the service area. If the distance is less than or equal to the configured radius, the walker is inside.
Impact
- Walks recorded in areas without signal now reach the server when the connection returns.
- The walker sees the connection status in real time, with a count of pending events.
- The walker’s service area is visible on the map as a circle, and geofence verification works even offline.
- The interface became readable under sunlight, which matters when the user is actually on the street.
Next steps
- Test the offline queue with real scenarios of prolonged connection loss.
- Add automated tests for the geofence Haversine fallback.
- Evaluate whether the 15-second ping interval is appropriate for battery on older devices.
Related entries
Same project, same ground — the stories connect.
Next step
If this was useful, there is more: I write about the same ground in the other projects, and the store holds the tools I use here.
Done — I will send the next article when it is out.
No spam, one-click unsubscribe. I am also here: Skills store · Portfolio