Offline - Connection Status Monitoring
Prerequisites
- Runtime: None required (pure CSS/JS library)
- Browser Support: Modern Chrome, Firefox, Safari, and IE8+
- Dependencies: None
Installation
-
Download the files:
- JavaScript: offline.min.js
- CSS Theme: Choose from available themes
- Language files: Optional, from languages directory
-
Include in your HTML:
<link rel="stylesheet" href="path/to/offline-theme-chrome.css"> <script src="path/to/offline.min.js"></script> -
Optional: Include language file if needed:
<script src="path/to/offline-lang-en.js"></script>
Configuration
Basic Options
Set configuration options after including the script:
Offline.options = {
checkOnLoad: false, // Check connection on page load
interceptRequests: true, // Monitor AJAX requests
reconnect: {
initialDelay: 3, // Initial delay before reconnect (seconds)
delay: (1.5 * last delay, capped at 1 hour)
},
requests: true, // Store and remake failed requests
game: false // Enable snake game (requires js/snake.js)
};
Connection Check Configuration
Change the URL used for connection testing:
Offline.options = {
checks: {
xhr: {
url: '/connection-test' // Custom endpoint for connection testing
}
}
};
For cross-domain testing, use the image method:
Offline.options = {
checks: {
image: {
url: 'my-image.gif'
},
active: 'image'
}
};
Build & Run
Development
No build process required. Simply include the files in your HTML as shown in the Installation section.
Production
- Ensure all files are minified (already provided as
offline.min.js) - Host the CSS, JS, and any language files on your production server
- Update file paths in your HTML to point to production locations
Deployment
Since this is a client-side library, deployment is straightforward:
- Static Hosting: Upload files to any static hosting service (Netlify, Vercel, GitHub Pages, AWS S3)
- CDN: Consider hosting the JS file on a CDN for faster loading
- Package Managers: Available via npm and other package managers if you prefer to include it in your build process
Troubleshooting
Common Issues
-
404 Errors in Console:
- Default connection check looks for
/favicon.ico - Solution: Either add a favicon or configure a custom endpoint:
Offline.options = {checks: {xhr: {url: '/connection-test'}}};
- Default connection check looks for
-
CORS Issues:
- Connection check must be same-origin (same protocol, domain, and port)
- Solution: Add
Access-Controlheaders to your endpoint or use the image method for cross-domain checks
-
No Visual Indicator:
- Ensure you've included the CSS theme file
- If using only the JavaScript API, no UI will be shown (this is expected)
-
Connection Detection Not Working:
- Some browsers (Safari, old IE) don't support offline events
- Offline falls back to less accurate detection methods in these cases
Testing
- Simulate Offline: Disconnect your internet connection or use the simulator
- Force Connection States: Set
localStorage.OFFLINE_SIMULATEto'up'or'down'for testing
Events
Listen for connection status changes:
Offline.on('up', function() {
console.log('Connection restored');
});
Offline.on('down', function() {
console.log('Connection lost');
});