Offline Support for scorm-again
This document explains how to use the offline support feature in scorm-again, which allows SCORM modules to function without an active internet connection and sync data back to the LMS when connectivity is restored.
Overview
The offline support feature enables:
- Running SCORM modules locally on a device without network connectivity
- Automatically storing SCORM data locally when offline
- Synchronizing data back to the LMS when the network connection is re-established
- Ideal for mobile applications, remote areas, or unreliable network conditions
Configuration
To enable offline support, you need to set several options when creating your SCORM API:
// Example configuration
const settings = {
// Standard settings
lmsCommitUrl: "https://your-lms.com/api/scorm/commit",
autocommit: true,
// Offline support settings
enableOfflineSupport: true, // Enable offline support
courseId: "COURSE-12345", // Unique identifier for the course
syncOnInitialize: true, // Attempt to sync data when initializing API
syncOnTerminate: true, // Attempt to sync data when terminating API
maxSyncAttempts: 5 // Maximum number of attempts to sync an item
};
// Create the API with offline support
const api = new Scorm12API(settings);Offline Support Settings
| Setting | Default | Description |
|---|---|---|
enableOfflineSupport | false | Master switch to enable/disable offline functionality |
courseId | "" | Unique identifier for the course or content module |
syncOnInitialize | true | Whether to try syncing offline data when the API initializes |
syncOnTerminate | true | Whether to try syncing offline data when the API terminates |
maxSyncAttempts | 5 | Maximum number of sync attempts for each data item |
How It Works
Automatic Mode Switching
The API automatically detects network connectivity status and switches between online and offline modes:
- When online, data is sent directly to the LMS as usual
- When offline, each commit is queued in localStorage in the configured
dataCommitFormat, with the course id and the learner id - If the same course and learner still have unsynced commits when
Initializeruns, the API loads them, oldest first, over any data you loaded from the LMS. Only values the SCO can write are restored. LMS-controlled values, such as the learner id, keep the values you loaded, andexitandsession_timeare not restored. Commits in theparamsformat are not restored. - When connection is restored, the commits for the current course and learner are synchronized back to the LMS. The stored copy for a course is removed once all of its commits have synced.
- Commits queued by older versions have no learner id. They are synced for their course but never restored.
Synchronization Events
Each sync run emits OfflineDataSyncing when it starts, then one result event:
OfflineDataSyncedwhen the LMS accepted every commit in the runOfflineDataSyncFailedwhen a commit failed and stays queued, a commit was dropped aftermaxSyncAttempts, or the run threw an error
This applies to every sync, whether Initialize, Commit, Terminate or a network status change started it. No events fire when a sync is skipped because nothing is queued for the current course and learner, the device is offline, or another sync is already running.
// Listen for offline data sync events
api.on('OfflineDataSynced', () => {
console.log('Successfully synchronized offline data to LMS');
});
// Listen for online/offline status changes
window.addEventListener('online', () => {
console.log('Device is back online, scorm-again will attempt to sync');
});
window.addEventListener('offline', () => {
console.log('Device is offline, scorm-again will store data locally');
});Custom Network Status Events
For mobile applications and other environments where the browser's navigator.onLine may not accurately reflect network status, scorm-again provides a custom event API to programmatically update network status.
Using the Custom Event
The recommended way for mobile apps to communicate network status changes is through the scorm-again:network-status custom event:
// Notify scorm-again when the device goes online
window.dispatchEvent(new CustomEvent('scorm-again:network-status', {
detail: { online: true }
}));
// Notify scorm-again when the device goes offline
window.dispatchEvent(new CustomEvent('scorm-again:network-status', {
detail: { online: false }
}));Benefits of the Custom Event API
- More Reliable: Mobile network detection APIs are often more accurate than browser
navigator.onLine - Clean Integration: No need to access internal service properties
- Future-Proof: Works across all versions and doesn't depend on internal API structure
- Standards-Based: Uses standard DOM CustomEvent API
Event Details
The custom event accepts a detail object with the following property:
| Property | Type | Description |
|---|---|---|
online | boolean | true if device is online, false if offline |
When the event is dispatched with online: true, scorm-again will automatically attempt to sync any queued offline data.
Mobile Applications Integration
To use offline support in a mobile application:
- WebView Integration: Use a WebView to load your SCORM content and scorm-again
- Configuration: Set up the API with offline support enabled
- Storage Access: Ensure your WebView allows localStorage access
- Network Handling: Use the custom event API to communicate network status changes
React Native Example
The React Native guide shows the full setup: the shared bridge script, running it at document start, and sending connectivity changes from native code. Its configuration sets lmsCommitUrl, enableOfflineSupport and courseId. Without a commit URL, commits are neither sent nor queued.
Detailed Mobile Framework Examples
For more comprehensive examples of implementing offline support in various mobile frameworks, please refer to these detailed guides:
- React Native Implementation
- Flutter Implementation
- iOS Native Implementation
- Native Android Implementation
- Xamarin/MAUI Implementation
- Kotlin Multiplatform Implementation
Limitations and Considerations
- Storage Limits: localStorage typically has a 5-10MB limit, which may affect large courses
- Security: Data is stored unencrypted in localStorage by default
- Data Persistence: Mobile apps may clear localStorage in certain scenarios
- Conflict Resolution: There is no conflict handling. Queued commits are replayed one at a time, in the order they were stored
- Connectivity Detection: Some browsers may have delayed connectivity status updates
Best Practices
- Always provide a meaningful
courseIdto properly identify offline data - Consider implementing additional local storage mechanisms for very large courses
- Test thoroughly in various connectivity scenarios
- Implement proper error handling for sync failures
- Consider implementing your own encryption for sensitive data
Troubleshooting
If you encounter issues with offline support:
- Data Not Syncing: Verify network connectivity and check browser console for errors
- Storage Errors: Check localStorage limits and browser permissions
- Course Not Loading Offline: Ensure all resources are locally available
- Mobile Integration Issues: Verify WebView configuration and JavaScript bridging