PWA Helpers

0.9.1 · active · verified Tue Apr 21

pwa-helpers is a collection of small, focused helper methods and mixins designed to reduce boilerplate when building Progressive Web Apps. It offers utilities for common PWA features such as client-side routing, network connectivity detection, dynamic metadata updates for SEO and social sharing, and media query monitoring. The current stable version is 0.9.1, with recent updates including TypeScript support introduced in v0.9.0. This library is designed to be framework-agnostic, providing vanilla JavaScript functions that can integrate with any web application stack, differentiating itself by its minimalist approach rather than being a comprehensive framework.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic routing, dynamic metadata updates, and network connectivity detection using `pwa-helpers`. It includes both initial setup and simulated programmatic navigation, showing how to integrate these helpers into a client-side application structure, and handle network status changes.

import { installRouter } from 'pwa-helpers/router.js';
import { updateMetadata } from 'pwa-helpers/metadata.js';
import { installOfflineWatcher } from 'pwa-helpers/network.js';

// A dummy store and navigate action for the example
const store = {
  dispatch: (action) => console.log('Dispatching action:', action)
};
const navigate = (location) => ({ type: 'NAVIGATE', payload: location.pathname });

function handleNavigation(location, event = null) {
  console.log('Navigated to:', location.pathname);
  // Example of integrating with a Redux-like store
  store.dispatch(navigate(location));

  // Update metadata based on the current route
  updateMetadata({
    title: `My PWA - ${location.pathname.substring(1) || 'Home'}`, 
    description: `Content for ${location.pathname}`, 
    url: window.location.href
  });

  if (event && event.type === 'click') {
    window.scrollTo(0, 0);
    console.log('Scrolled to top after click.');
  }
}

// Install the router
installRouter(handleNavigation);

// Install the offline watcher
installOfflineWatcher((offline) => {
  const message = offline ? 'You are currently OFFLINE' : 'You are currently ONLINE';
  console.log(message);
  // Optionally update UI based on connectivity
  const statusElement = document.getElementById('network-status');
  if (statusElement) statusElement.textContent = message;
});

// Simulate a programmatic navigation
setTimeout(() => {
  window.history.pushState({}, '', '/about');
  handleNavigation(window.location);
}, 2000);

// Create a simple div for network status (if running in a browser environment)
if (typeof document !== 'undefined') {
  const statusDiv = document.createElement('div');
  statusDiv.id = 'network-status';
  statusDiv.textContent = 'Checking network status...';
  document.body.appendChild(statusDiv);
}

view raw JSON →