Overview
MotorDesk implements a robust offline-first architecture that allows users to continue working even without internet connectivity. All data is persisted locally and synchronized automatically when the connection is restored.Storage Architecture
LocalForage Configuration
The application uses LocalForage as an abstraction layer over IndexedDB for reliable local storage:LocalForage automatically falls back to WebSQL or localStorage if IndexedDB is not available, ensuring compatibility across browsers.
Redux Persist Configuration
Redux Persist is configured to automatically save critical application state to IndexedDB:Persisted State
Only
auth and sales slices are persisted. This selective approach keeps storage lean while ensuring critical data survives page refreshes and offline periods.Persistence Flow
Sync Status Management
Sale Sync States
Each sale in the system has async_status field that tracks its synchronization state:
PENDING
PENDING
Sale was created offline or failed to sync. Will be synchronized on next reconnection.
SYNCED
SYNCED
Sale has been successfully synchronized with the backend server.
Adding Sales Offline
When a sale is created, it’s immediately added to local state withPENDING status:
Synchronization Logic
Redux Saga Implementation
TherootSaga.ts file orchestrates all synchronization logic using Redux Saga:
Network Detection
The sync process checks network status before attempting synchronization:Online Detection
Uses the
navigator.onLine property to detect network connectivity. Note that this only detects if the device has a network connection, not if the backend is reachable.Reconnection Handling
Sync Triggers
The saga listens for two primary triggers to initiate synchronization:On Sale Creation
When a sale is created, attempt immediate sync if online
On Reconnection
When the app detects reconnection, sync all pending sales
Implementing Reconnection Detection
To dispatch theapp/RECONNECTED action, add network listeners in your app:
Middleware Configuration
Redux Store Setup
The store is configured with custom middleware to support both Redux Saga and Redux Persist:Thunk middleware is disabled in favor of Redux Saga. Redux Persist actions are excluded from serialization checks to prevent console warnings.
Saga Middleware Initialization
Sync Flow Diagram
State Rehydration
On Application Load
When the application starts, Redux Persist automatically rehydrates state from IndexedDB:persistor should be passed to PersistGate in your app root:
Loading State
The
loading prop can be set to a loading component that displays while state is being rehydrated from storage.Best Practices
Selective Persistence
Selective Persistence
Only persist essential state slices. In MotorDesk, only
auth and sales are persisted to minimize storage usage and improve performance.Optimistic UI Updates
Optimistic UI Updates
Always update the UI immediately when users take actions, even if the backend sync happens later. This provides better user experience.
Sync Status Visibility
Sync Status Visibility
Show users which items are pending synchronization with visual indicators (badges, icons) in the UI.
Error Handling
Error Handling
Implement proper error handling in sagas to prevent sync failures from crashing the app. Log errors for debugging.
Conflict Resolution
Conflict Resolution
Implement conflict resolution strategies for cases where local data conflicts with server data during sync.
Storage Capacity
Testing Offline Functionality
To test offline functionality during development:Next Steps
State Management
Learn more about Redux slices and saga patterns
Architecture
Understand the overall system architecture
