Skip to content

lorb-studio/offline-auto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lorb.studio

@lorb/offline-auto

Your web app works offline. Three lines, zero config.

Automatic caching. Static assets (JS, CSS, images, fonts) are cached on first visit and served instantly on return. API calls go network-first with offline fallback.

Request queue. POST, PUT, DELETE requests that fail while offline are saved and automatically replayed when connectivity returns.

No setup. No Workbox, no config file, no service worker file to host, no build plugin. Just init().

import { init } from '@lorb/offline-auto';

init();
// Your app now works offline.

Install

npm install @lorb/offline-auto

What you can do

Make any web app work offline

import { init } from '@lorb/offline-auto';

init();
// Static assets: cached automatically (cache-first)
// API calls: network-first, falls back to cache when offline
// Failed writes: queued and replayed on reconnect

Exclude specific paths from caching

init({
  excludePaths: ['/api/auth', '/api/realtime'],
});

Respond to connectivity changes

init({
  onStatusChange: (online) => {
    document.querySelector('.status').textContent =
      online ? 'Connected' : 'Working offline';
  },
});

Version your cache

init({ cacheName: 'my-app-v2' });
// Old caches from previous versions are automatically cleaned up.

Remove offline support

const cleanup = init();

// Later: unregister the service worker and clear all caches
cleanup();

How it works

Request type Strategy Offline behavior
Static assets (.js, .css, .png, .woff2, ...) Cache-first Served from cache
GET API calls Network-first Falls back to cached response
POST / PUT / DELETE Network-first Queued in IndexedDB, replayed on reconnect

Storage: Uses OPFS when available (larger quota), falls back to Cache API.

Compatibility

Chrome and Edge. Firefox and Safari don't support blob-URL service worker registration.

import { isSupported } from '@lorb/offline-auto';

if (isSupported()) {
  init();
}

API

Export Description
init(options?) Register service worker. Returns cleanup() function
isSupported() Check browser compatibility

Options:

Option Type Default Description
cacheName string 'offline-auto-v1' Cache storage name
excludePaths string[] [] URL paths to skip
onStatusChange (online: boolean) => void Connectivity change callback

License

𖦹 MIT — Lorb.studio

About

Your web app works offline. Three lines, zero config

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors