Skip to content

Agent Directive: Offline Synchronisation Engine

Objective

Implement the background synchronisation logic for the Astro PWA. The application must hydrate its local Dexie database from the Cloudflare edge API when online and queue local modifications when offline.

Execution Sequence

1. Implement the Sync Logic

  • Create a new file at app/src/lib/sync.ts.
  • Implement a hydrateSchedule() function that calls GET /api/data from the Cloudflare Worker and overwrites the local Dexie festivals, stages, and bands tables with the master dataset.
  • Implement a pushLocalChanges() function that queries Dexie for any user routes where sync_status = 'pending', pushes them to the Cloudflare API, and updates their status to 'synced' upon success.

2. Integrate Network Event Listeners

  • Update app/src/pages/index.astro.
  • Add client-side JavaScript to listen for window.addEventListener('online') and window.addEventListener('offline').
  • Trigger pushLocalChanges() automatically when the device regains connectivity.
  • Trigger hydrateSchedule() on initial page load if the Dexie tables are empty and the device is online.