# @jswork/sw-runtime

> Service worker runtime for workbox.

Latest version **1.0.13** (published 2024-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @jswork/sw-runtime
pnpm add @jswork/sw-runtime
yarn add @jswork/sw-runtime
bun add @jswork/sw-runtime
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.13 |
| Published | 2024-09-16 |
| First published | 2024-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 101.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | afeiship |
| Keywords | active, cache, cli, install, pwa, service-worker, sw, workbox |

## Links

- npm: https://www.npmjs.com/package/@jswork/sw-runtime
- npm.io page: https://npm.io/package/@jswork/sw-runtime

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.13 (latest) — 2024-09-16
- 1.0.12 — 2024-08-30
- 1.0.11 — 2024-08-30
- 1.0.10 — 2024-02-18
- 1.0.9 — 2024-02-18
- 1.0.8 — 2024-02-18
- 1.0.7 — 2024-01-28
- 1.0.6 — 2024-01-28
- 1.0.5 — 2024-01-28
- 1.0.4 — 2024-01-28
- 1.0.3 — 2024-01-28
- 1.0.2 — 2024-01-19
- 1.0.1 — 2024-01-19

## README

# sw-runtime
> Service worker runtime for workbox.

[![version][version-image]][version-url]
[![license][license-image]][license-url]
[![size][size-image]][size-url]
[![download][download-image]][download-url]

## installation
```shell
npm install @jswork/sw-runtime
```

## skipWaiting
```js
// case1: skipWaiting true: will be update immediately
module.exports = {
  maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
  globDirectory: 'build/',
  globPatterns: ['**/*.{js,png,json,txt,css,map}'],
  globIgnores: ['*/fallback.js'],
  skipWaiting: true,
  clientsClaim: true,
  //...
};

// case2: skipWaiting false: will be update when you click sw-tips `reload` button
module.exports = {
  maximumFileSizeToCacheInBytes: 100 * 1024 * 1024,
  globDirectory: 'build/',
  globPatterns: ['**/*.{js,png,json,txt,css,map}'],
  globIgnores: ['*/fallback.js'],
  skipWaiting: false,
  clientsClaim: false,
  //...
};
```

## usage
```js
import SwRuntime from '@jswork/sw-runtime';

// App Did Mount, config sw
window.onload = function () {
  SwRuntime.install({
    autoUpdate: true,
    autoUpdateInterval: 5 * 1000,
    onAutoUpdate: function () {
      console.log('SW Event:', 'onAutoUpdate');
    },
    onUpdateReady: function ({ context }) {
      console.log('SW Event:', 'onUpdateReady');
      setTimeout(function () {
        console.log('SW Event:', 'do applyUpdate');
        // Need to reload page, or it will be pending.
        context.applyUpdate().then(()=>{
          window.location.reload();
        });
      }, 1000);
    },
  });
};
```

## todos
1. unpkg/jsdelivr cdn zero cache?(not ok)
2. auto update interval(ok)

## license
Code released under [the MIT license](https://github.com/afeiship/sw-runtime/blob/master/LICENSE.txt).

[version-image]: https://img.shields.io/npm/v/@jswork/sw-runtime
[version-url]: https://npmjs.org/package/@jswork/sw-runtime

[license-image]: https://img.shields.io/npm/l/@jswork/sw-runtime
[license-url]: https://github.com/afeiship/sw-runtime/blob/master/LICENSE.txt

[size-image]: https://img.shields.io/bundlephobia/minzip/@jswork/sw-runtime
[size-url]: https://github.com/afeiship/sw-runtime/blob/master/dist/index.min.js

[download-image]: https://img.shields.io/npm/dm/@jswork/sw-runtime
[download-url]: https://www.npmjs.com/package/@jswork/sw-runtime

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