Initial Caching Process
Overview
When a user enables offline caching, the system performs an initial caching process to download all required data for offline use. During this process, the application operates in online mode until caching completes. This ensures data consistency and allows users to continue working while caching progresses.
Caching Phases
The initial caching process consists of several sequential phases:
┌─────────────────────────────────────────────────────────────────────┐
│ Initial Caching Process │
│ │
│ 1. Static App Caching │
│ └─ Service worker precaches JS, CSS, fonts, images │
│ │
│ 2. Map Tile Caching │
│ └─ Vector tiles for configured region │
│ │
│ 3. Document Caching │
│ └─ Documents with offline access keys │
│ │
│ 4. File/Attachment Caching │
│ └─ Files referenced by cached documents │
│ │
│ 5. Custom Function Caching │
│ └─ Address database, custom data sources │
│ │
│ ═══════════════════════════════════════════════════════════════ │
│ Initial Caching Complete │
│ └─ Device can now operate offline │
└─────────────────────────────────────────────────────────────────────┘
Enabling Caching
User Interface
Users enable caching through the user menu:
- Click user icon in header
- Select "Enable Offline"
- Navigate to caching progress page
Caching Enable Template
Configure a template to show during caching:
{
"clientConfiguration": {
"offline": {
"cachingEnableTemplate": [
{
"documentId": "9ed4c3fb-1a59-49dc-be26-7e40e95ab194",
"name": "Enabling Caching"
}
]
}
}
}
This template typically contains the sc-offline-status component to show progress.
Online Mode During Caching
Behavior
While initial caching is in progress:
- All data reads come from the network (RestDataService)
- All data saves go directly to the server
- Documents are not written to IndexedDB
- The device cannot operate offline
Implementation
The DataService checks caching status before using offline storage:
const offlineMode = this.offlineStatusService.isCachingEnabled()
&& this.offlineStatusService.isInitialCachingComplete();
if (offlineMode) {
// Use IndexedDB
return this.indexedDBService.getDocument(documentId);
} else {
// Use network
return this.restDataService.getDocument(documentId);
}
Initial Caching Completion
Definition
Initial caching is complete when all documents with offline access keys that existed at the time caching was enabled have been cached.
Cut-off Time
The system records the timestamp when caching was enabled:
// Documents are cached until serverUpdatedDate < cachingEnabledTime
const cachingEnabledTime = offlineStatusService.getCachingEnabledTime();
// Initial caching complete when no more documents
// with serverUpdatedDate < cachingEnabledTime remain
Continuous Document Streams
If documents are being saved continuously on the server:
- Initial caching only includes documents before the cut-off time
- Documents saved after caching was enabled are synced separately
- This prevents initial caching from never completing
Completion Message
The "Initial Caching Complete" message displays when:
- All documents before cut-off time are cached
- All map tiles are cached
- All static files are cached
- No errors occurred during caching
Connection Loss Handling
Automatic Resume
If network connection is lost during caching:
- Blue notification appears: "Loaded offline data"
- Caching pauses automatically
- When connection is regained, caching resumes
- Progress continues from where it stopped
Configuration
Configure retry interval for network loss:
{
"clientConfiguration": {
"offline": {
"cacheRetryInterval": 20000
}
}
}
Default retry interval is 10 seconds (10000ms).
Recovery Behavior
| Phase | Recovery Action |
|---|---|
| Static App | Service worker handles |
| Map Tiles | Resumes from current tile |
| Documents | Resumes from last sync time |
| Files | Retries failed files |
Progress Tracking
Progress Indicators
The sc-offline-status component displays:
| Metric | Description |
|---|---|
| Documents cached | Count of documents in IndexedDB |
| Documents to cache | Total documents matching offline keys |
| Files cached | Count of attachment files cached |
| Tiles cached | Count of map tiles cached |
Internal State
// OfflineStatusService
class OfflineStatusService {
initialCachingComplete: boolean = false;
cachingEnabled: boolean = false;
cachingEnabledTime: Date;
pageDownloadProgress: {
pageNumber: number;
loaded: number;
total: number;
};
}
Checking Caching Status
// Check if device can operate offline
if (offlineStatusService.isInitialCachingComplete()) {
// Safe to use offline features
}
// Check caching is enabled but not complete
if (offlineStatusService.isCachingEnabled() &&
!offlineStatusService.isInitialCachingComplete()) {
// Still caching, use online mode
}
Refreshing During Caching
Behavior
If the user refreshes the page during caching:
- Caching progress is preserved
- Page reloads in online mode
- Caching resumes automatically
- Progress display updates
Page Navigation
Users can navigate away from the caching page:
- Caching continues in the background
- App operates in online mode
- Progress is tracked in OfflineStatusService
- User can return to see progress
Error Handling
Error Types
| Error | Behavior |
|---|---|
| Network timeout | Retry after interval |
| Server error (5xx) | Retry after interval |
| Auth failure (401) | Stop caching, require re-login |
| Storage full | Stop caching, show error |
Error Recovery
try {
await this.loadDataCache();
} catch (err) {
// Don't mark as complete on error
this.offlineStatusService.initialCachingComplete = false;
// Schedule retry
setTimeout(() => this.retryCache(), this.retryInterval);
}
Storage Space
If browser storage is insufficient:
- Error message displayed
- Caching stops
- User prompted to clear space
- Can retry after clearing
Configuration Options
Caching Page Size
Control how many documents are fetched per request:
{
"clientConfiguration": {
"offline": {
"syncPageSize": 10000
}
}
}
Retry Interval
Time to wait before retrying after connection loss:
{
"clientConfiguration": {
"offline": {
"cacheRetryInterval": 20000
}
}
}
Best Practices
Preparation
- Ensure stable network - Initial caching requires significant data transfer
- Allow sufficient time - Large datasets may take extended time
- Check storage space - Verify browser has adequate storage
- Stay connected - Avoid switching networks during caching
For Administrators
- Configure offline keys - Only mark necessary documents for offline
- Limit document count - Fewer documents = faster caching
- Use appropriate page size - Balance between speed and memory
- Test caching flow - Verify with representative users
Monitoring
- Watch progress - Ensure count increases steadily
- Check for errors - Monitor console for issues
- Verify completion - Confirm "Initial Caching Complete" appears
- Test offline - Verify app works when disconnected
Troubleshooting
Caching Never Completes
Causes: - Continuous document saves on server - Network interruptions - Storage space issues
Solutions:
1. Check for automated document saves
2. Verify network stability
3. Clear browser storage
4. Check cacheRetryInterval setting
"Initial Caching Complete" Shows Prematurely
Causes: - Error during caching process - Incorrect completion logic
Solutions: 1. Check console for errors 2. Verify document count matches expected 3. Clear cache and retry
Progress Resets on Refresh
Causes: - State not persisted correctly - Service worker issue
Solutions: 1. Don't refresh during caching 2. Check service worker is active 3. Verify keyValueStorage has correct state
Memory Issues During Caching
Causes: - Large document batches - Browser memory limits
Solutions:
1. Reduce syncPageSize
2. Close other tabs
3. Use supported browser version
Related Documentation
- 101-Offline-Architecture.md - System overview
- 103-Service-Workers.md - Static file caching
- 104-Offline-Syncing.md - Data synchronization
- 108-Offline-UI-Components.md - Status components
- 109-Configuration-Reference.md - All configuration options