02 Service Worker
Service Worker & Offline Queue
OnTheGo uses a Service Worker to enable Progressive Web App (PWA) features like offline support and background sync. Push notifications are planned and may be limited by browser/device support.
What is a Service Worker?
A service worker is a script that runs in the background, separate from your main browser tab. It enables:
| Feature | Benefit |
|---|---|
| Offline Support | Continue viewing cached orders without internet |
| Background Sync | Queue actions while offline, sync when reconnected |
| Push Notifications | Planned (when enabled) |
| Faster Loading | Cache assets for instant page loads |
How Offline Queue Works
When you lose internet connection, OnTheGo automatically queues your write actions:
What Gets Queued
| Action | Queued When Offline |
|---|---|
| Status updates | ✅ Yes |
| New orders (Quick Entry) | ✅ Yes |
| Journal entries | ✅ Yes |
| Invoice creation | ✅ Yes |
| Order assignment | ✅ Yes |
What Doesn't Queue
| Action | Behavior |
|---|---|
| Authentication | Requires connection |
| File uploads | Skipped |
| Sending notifications | Skipped |
Queue Indicators
When you're offline, you'll see a banner at the top:
| Banner Color | Meaning |
|---|---|
| 🟡 Amber | You're offline • N changes queued |
| 🔵 Blue | Syncing... |
| (Hidden) | Online with no pending changes |
Automatic Sync
When you regain connection:
- The banner turns blue ("Syncing...")
- Queued actions are sent to the server (oldest first)
- Completed actions are removed from queue
- Banner disappears when sync is complete
Sync Priority
Actions are processed in priority order:
- High priority - Urgent status changes
- Normal priority - Standard updates (default)
- Low priority - Non-critical updates
Within each priority, older actions sync first.
PWA Installation
Installing OnTheGo as an app provides the best experience:
How to Install
- Open OnTheGo in your mobile browser
- Look for the "Install" or "Add to Home Screen" prompt
- Tap to install
- OnTheGo appears as an app icon
Benefits of Installing
| Feature | Browser Tab | Installed App |
|---|---|---|
| Push notifications | ⚠️ May not work when closed | ✅ Works reliably |
| Offline caching | ✅ Yes | ✅ Yes |
| Full-screen mode | ❌ No | ✅ Yes |
| App icon | ❌ No | ✅ Yes |
Troubleshooting
"You're Offline" Banner Won't Go Away
Cause: No internet connection or sync stuck
Solutions:
- Check your device's internet connection
- Wait 30 seconds - sync has a fail-safe timeout
- Refresh the page (pull down on order list)
- If persistent, clear the cache (see below)
Changes Not Syncing
Cause: Network error, server issue, or conflict
Solutions:
- Check you have internet connection
- Look for error messages in browser console
- Try refreshing the page
- If conflict detected, you may need to manually resolve
App Not Updating
Cause: Service worker cache is stale
Solutions:
- Close all OnTheGo tabs
- Wait 30 seconds
- Reopen OnTheGo
- Or force refresh:
Ctrl+Shift+R(Windows) /Cmd+Shift+R(Mac)
Clear Service Worker Cache
If you're experiencing persistent issues:
- Open browser DevTools (
F12) - Go to Application tab
- Click Service Workers in sidebar
- Click Unregister next to OnTheGo service worker
- Click Clear storage > Clear site data
- Refresh the page
Check Service Worker Health
View service worker status and logs:
- Open browser DevTools (
F12) - Go to Application tab
- Click Service Workers in sidebar
- View status and log entries
Or use the health endpoint:
/onthego/sw-health
Returns:
{
"version": "2.3",
"uptime": 1234567,
"cacheSize": 42,
"metrics": {
"fetchCount": 150,
"cacheHitCount": 120,
"cacheMissCount": 30,
"errorCount": 0
},
"logs": [...]
}
Conflict Resolution
If multiple people edit the same order while one is offline:
- You'll see a conflict notification
- Check the order for current state
- Re-apply your changes if needed
- Contact support if data appears incorrect
Browser Compatibility
| Browser | Service Worker | Offline Queue | Push Notifications |
|---|---|---|---|
| Chrome | ✅ | ✅ | ✅ |
| Edge | ✅ | ✅ | ✅ |
| Firefox | ✅ | ✅ | ✅ |
| Safari | ✅ | ✅ | ✅ (iOS 16.4+) |
| Opera | ✅ | ✅ | ✅ |
Technical Details
For developers or advanced users:
Cache Strategy
| Resource Type | Strategy |
|---|---|
| Navigation (pages) | Network First, fallback to cache |
| Static assets (images, CSS) | Cache First, validate in background |
| API calls | Network Only (never cached) |
Cache Duration
- Cached pages expire after 24 hours
- Service worker checks for updates on each navigation
- Max 100 entries in cache (oldest removed first)
Idempotency Keys
Each queued request includes an idempotency key that prevents duplicate execution if the request is sent multiple times during sync.
Data Persistence
- Cache timestamps stored in IndexedDB (survives SW restart)
- Queue persisted in IndexedDB (survives page refresh)
- Metrics tracked in SW memory (reset on SW restart)
See Also
- Push Notifications - Setting up alerts
- OnTheGo - Mobile field platform
- User Profile - Account settings