# swkit

> Service worker helper functions

Latest version **1.0.3** (published 2017-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install swkit
pnpm add swkit
yarn add swkit
bun add swkit
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-04-27 |
| First published | 2016-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Erin Dachtler |
| Maintainers | download |

## Links

- npm: https://www.npmjs.com/package/swkit
- Repository: https://github.com/download13/swkit
- Homepage: https://github.com/download13/swkit#readme
- Issues: https://github.com/download13/swkit/issues
- npm.io page: https://npm.io/package/swkit

## Dependencies (1)

- [route-parser](https://npm.io/package/route-parser.md) git+ssh://git@github.com/download13/route-parser.git#ce9e713e4846f86673577a9e9057b480682a1787

## Recent versions

- 1.0.3 (latest) — 2017-04-27
- 1.0.2 — 2017-03-24
- 1.0.1 — 2017-03-22
- 1.0.0 — 2017-02-18
- 0.3.2 — 2017-02-13
- 0.3.1 — 2017-02-13
- 0.3.0 — 2017-01-09
- 0.2.0 — 2017-01-09
- 0.1.0 — 2016-12-26

## README

# swkit

Helpers that make working with service workers a little easier.

## install

`npm install swkit` or `yarn add swkit`

## Example

```javascript
import {
	on,
	cacheAll,
	createRouter,
	networkFirst
} from 'swkit';


const router = createRouter();

// Create middleware that updates `precache` and falls back to only cache
// in the absence of a network connection.
const precacheNetworkFirst = networkFirst('precache');

router.get('/', precacheNetworkFirst);

router.get('/style.css', precacheNetworkFirst);

router.get('/randomnumber', request => {
	return new Response(Math.random().toString().substr(2));
});

router.get('/asyncrandomnumber', request => {
	return new Promise((resolve, reject) => {
		resolve(new Response(Math.random().toString().substr(2)));
	});
});

// Middleware takes a Request object, and a parameters object.
// params holds the values of the replaced url tokens (in this case, `id`).
router.get('/items/:id', (req, params) => {
	// Router expects the middleware to return a Promise or a Response
	return new Response('This is item ' + params.id);
});

on('fetch', router.dispatch);

on('install', e => {
  e.waitUntil(
    cacheAll('precache', ['/', '/style.css'])
    .then(skipWaiting())
  );
});

on('activate', e => {
  e.waitUntil(clients.claim());
});

```

`dist/index.js` is in [UMD](https://github.com/umdjs/umd) format so it can be imported by a build process, or simply used via [importScripts](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts). If imported with `importScripts`, the contents of the library will be found under the global object `swkit`.

---
_Source: https://npm.io/package/swkit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
