@lorb/offline-auto
v0.1.0
Published
3 lines. Your web app works offline. No Workbox, no config, no headache.
Maintainers
Readme
@lorb/offline-auto
3 lines. Your web app works offline. No Workbox, no config, no headache.
Install
npm install @lorb/offline-autoUsage
import { init } from '@lorb/offline-auto';
// That's it. Your app works offline now.
init();With options
const cleanup = init({
cacheName: 'my-app-v1',
excludePaths: ['/api/auth'],
onStatusChange: (online) => {
console.log(online ? 'Back online' : 'Offline');
},
});
// Later: unregister SW and clear cache
cleanup();What it does
- Static assets (js, css, images, fonts) are cached automatically -- cache-first
- API calls use network-first with offline fallback
- Failed POST/PUT/DELETE requests are queued and replayed when back online
- Uses OPFS for storage when available (larger quota than Cache API)
- No build step. No config file. No service worker file to host
Why
Service workers are powerful but painful to set up. Workbox helps but still needs config files, build plugins, and strategy decisions. This is for when "just make it work offline" is the whole requirement.
Compatibility
Chrome and Edge only. Firefox and Safari reject blob-URL service worker registration.
Call isSupported() to check at runtime:
import { isSupported } from '@lorb/offline-auto';
if (isSupported()) {
init();
}API
init(options?): () => void
Registers a service worker and returns a cleanup function.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| cacheName | string | 'offline-auto-v1' | Cache storage name |
| excludePaths | string[] | [] | URL paths to never cache |
| onStatusChange | (online: boolean) => void | -- | Called on connectivity change |
isSupported(): boolean
Returns true if the current environment supports offline-auto.
License
MIT -- Lorb.studio
