JSPM

@lopatnov/namespace-pwa

3.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q30353F
  • License Apache-2.0

PWA utilities for @lopatnov/namespace. Service Worker registration, install prompt, network status, and update management.

Package Exports

  • @lopatnov/namespace-pwa

Readme

@lopatnov/namespace-pwa

npm license

PWA utilities for @lopatnov/namespace. Service Worker registration, install prompt, network status detection, and SW update management.

Install

npm install @lopatnov/namespace-pwa

Quick start

import {
  registerSW,
  onInstallPrompt,
  promptInstall,
  isOnline,
  onOffline,
  onOnline,
  onUpdateAvailable,
  activateUpdate,
} from '@lopatnov/namespace-pwa';

// Register Service Worker
registerSW('./sw.js');

// Detect network state
onOffline(() => document.getElementById('banner').hidden = false);
onOnline(() => document.getElementById('banner').hidden = true);

// Install prompt
onInstallPrompt(() => {
  document.getElementById('install-btn').hidden = false;
});
document.getElementById('install-btn').onclick = () => promptInstall();

// SW updates
onUpdateAvailable((reg) => {
  if (confirm('New version available. Reload?')) activateUpdate(reg);
});

API

Service Worker

Function Description
registerSW(url, options?) Register a Service Worker. Returns Promise<ServiceWorkerRegistration | undefined>

RegisterSWOptions: onSuccess(reg), onError(err).

Install prompt

Function Description
onInstallPrompt(cb) Subscribe to beforeinstallprompt. Returns unsubscribe function
promptInstall() Show the native install prompt. Returns Promise<InstallOutcome>

InstallOutcome: "accepted" | "dismissed" | "unavailable".

Network status

Function Description
isOnline() Returns current navigator.onLine
onOffline(cb) Subscribe to going offline. Returns unsubscribe function
onOnline(cb) Subscribe to coming back online. Returns unsubscribe function

SW updates

Function Description
onUpdateAvailable(cb) Called when a new SW is waiting to activate. Returns unsubscribe function
activateUpdate(reg) Send SKIP_WAITING message to the waiting SW

License

Apache-2.0 © lopatnov