npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tiny-serviceworker

v4.2.2

Published

Service Worker with automatic cache management for your PWA.

Downloads

31

Readme

tiny-serviceworker

Service Worker with automatic cache management for your PWA.

How do I use this?

  • Install from npm or clone from github.
  • Copy sw.js to root directory of web app.
  • Edit sw.js, renaming name & setting urls to the required files to run web app offline.
  • Increment the version integer any time a core asset changes to delete a stale cache.
  • Install sw.js in web app (see loader.js).
(function () {
	function log (arg) {
		console.type(`[my-app:${new Date().getTime()}] ${arg}`);
	}

	if ("serviceWorker" in navigator) {
		window.addEventListener("load", async () => {
			try {
				const registration = await navigator.serviceWorker.register("/sw.js", {scope: "/"});

				log(`type=serviceWorker, message="ServiceWorker registration successful with scope: ${registration.scope}"`);
			} catch (err) {
				log(`type=error, source=serviceWorker, message="ServiceWorker registration failed: ${err.message}"`);
			}
		});
	}
}());

Command Line Interface

Install with npm & use the cli to generate a service worker in the current directory.

$ npm i tiny-serviceworker -g
$ sw --name="myapp" --directories="assets/css,assets/js,assets/img" --loader=true

Parameters

name

Name of your PWA

announce (optional - default true)

Boolean to enable a client.postMessage("version_$version");

directories (optional)

Quoted comma delimited relative (from root) directory names to include in generated service worker

files (optional)

Quoted comma delimited relative (from root) files to include in generated service worker

increment (optional - default true)

Boolean to auto-increment version integer in sw.js

ignore (optional)

Quoted comma delimited relative (from root) directories or files to exclude; supports * for wildcards

hosts (optional)

Quoted comma delimited hostnames valid for caching requests

loader (optional - default false)

Boolean to generate loader.js script for sw.js

reload (optional - default false)

Boolean to force clients to reload when cache has gone stale

safari (optional - default true)

Boolean to load service worker in Safari browser (based on navigator.userAgent)

timeout (optional - default 1800)

Default cache TTL (seconds) on requested URLs. Does not apply to core assets!

version (optional - default 1)

Cache version. Increment when core assets change.

walk (optional - default true, requires 'directories')

Boolean to enable/disable walking directories for cache inclusion