Overview
The offline-first architecture ensures that:- All features work without internet connectivity
- Data is stored locally on the device
- The app can be installed like a native application
- Assets are cached for instant loading
Service Worker
Caches all app assets and enables offline operation
IndexedDB
Stores all products and sales data locally
PWA Install
Add to home screen on mobile or desktop
Cache Strategy
Cache-first approach with network fallback
Service Worker Architecture
The service worker implements a complete caching strategy:sw.js
The cache version (
CACHE) is incremented whenever assets change, forcing the service worker to update and clear old caches.Installation Phase
During installation, the service worker pre-caches all essential assets:sw.js
Activation and Cache Cleanup
Old caches are automatically removed when a new version activates:sw.js
Fetch Strategy
The service worker uses a cache-first strategy with network fallback:sw.js
Strategy Breakdown
- Check Cache First: Look for the requested resource in the cache
- Return Cached: If found, serve from cache immediately (no network delay)
- Fetch from Network: If not cached, try to fetch from the network
- Update Cache: Cache the network response for future use
- Fallback: If offline and resource not cached, serve the index page
Only GET requests are intercepted. POST, PUT, DELETE requests pass through directly to the network (though IndexedDB handles all data operations locally).
Service Worker Registration
The app registers the service worker on page load:js/shared.js
PWA Installation
The app can be installed on devices that support PWA installation:js/shared.js
Installation Flow
- Browser detects PWA is installable
beforeinstallpromptevent fires- Install button becomes visible in the header
- User clicks “Install App”
- Browser shows native install prompt
- App is added to home screen/application menu
- Install button hides automatically
Installation Requirements
Installation Requirements
For a PWA to be installable, it must:
- Be served over HTTPS (or localhost for development)
- Have a valid manifest.json with icons and name
- Have a registered service worker
- Meet the browser’s engagement heuristics
Local Data Storage
All application data is stored in IndexedDB:js/db.js
Storage Benefits
- No Server Required: All data stays on the device
- Fast Access: IndexedDB queries are extremely fast
- Large Capacity: Can store thousands of products and transactions
- Structured Data: Supports complex objects with indexes
- Transactional: ACID-compliant for data integrity
IndexedDB is persistent storage that survives browser restarts and even device reboots.
Offline Capabilities
What Works Offline
✅ All Core Features:- View dashboard and statistics
- Add, view, and delete products
- Process sales transactions
- Generate and print receipts
- View sales reports
- Export data to CSV
- Print PDF reports
- All products and sales are saved locally
- No data loss when offline
- Immediate read/write operations
- Complete interface loads from cache
- All pages navigate instantly
- Icons and styles cached
What Requires Network
❌ External Resources (if any):- External CDN scripts (app uses local CSS)
- Remote API calls (app doesn’t make any)
- Cloud sync (not implemented)
Cache Management
Updating the App
When you make changes and want to deploy a new version:-
Increment the cache version:
-
The service worker will:
- Install with the new cache version
- Pre-cache all assets again
- Activate and delete old caches
- Serve the new version
Manual Cache Clear
Users can clear the cache via browser settings:- Chrome: Settings → Privacy → Clear browsing data
- Firefox: Settings → Privacy & Security → Cookies and Site Data
- Safari: Settings → Safari → Clear History and Website Data
Clearing cache doesn’t delete IndexedDB data. Products and sales remain intact.
PWA Manifest
The manifest.json defines how the app appears when installed:manifest.json
Best Practices
For Developers
- Version Bumping: Always increment cache version after changes
- Asset List: Keep the ASSETS array updated with all files
- Testing Offline: Use Chrome DevTools → Application → Service Workers → Offline
- Cache Size: Monitor cache size to avoid exceeding browser limits
- Error Handling: Use
Promise.allSettledto handle partial cache failures
For Users
- Install the App: Get native app experience with offline support
- Initial Load: Ensure first visit completes to cache all assets
- Regular Use: App works seamlessly whether online or offline
- Data Backup: Export reports regularly as browser data can be cleared
Troubleshooting
Service Worker Not Updating
Service Worker Not Updating
Problem: Changes don’t appear after deploymentSolutions:
- Verify cache version was incremented
- Hard refresh (Ctrl+Shift+R or Cmd+Shift+R)
- Clear browser cache manually
- Unregister service worker in DevTools and reload
Install Button Not Showing
Install Button Not Showing
Data Not Persisting
Data Not Persisting
Problem: Products or sales disappearSolutions:
- Check browser storage settings (allow sites to save data)
- Verify IndexedDB isn’t being cleared by privacy extensions
- Ensure browser isn’t in private/incognito mode
- Check available storage space
Browser Support
- Chrome/Edge: Full PWA support including install
- Firefox: Service worker and caching work, limited install UI
- Safari: iOS 11.3+ supports PWA, add to home screen
- Opera: Full PWA support
All modern browsers support IndexedDB and service workers, ensuring the app works offline everywhere.
Related Pages
- Database API - Local data storage details
- Dashboard - Offline-capable metrics
- Installation Guide - Deploying the PWA