Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stagas/dynamic-cache

service worker where cache can be updated dynamically at runtime
https://github.com/stagas/dynamic-cache

browser cache dynamic imports service-worker

Last synced: 14 days ago
JSON representation

service worker where cache can be updated dynamically at runtime

Awesome Lists containing this project

README

        

dynamic-cache


service worker where cache can be updated dynamically at runtime


🔧 Install
· 🧩 Example
· 📜 API docs
· 🔥 Releases
· 💪🏼 Contribute
· 🖐️ Help

***

## Install

```sh
$ npm i dynamic-cache
```

## API

#### Table of Contents

* [constructor](#constructor)
* [Parameters](#parameters)
* [put](#put)
* [Parameters](#parameters-1)
* [register](#register)
* [Parameters](#parameters-2)

### constructor

[src/index.ts:58-62](https://github.com/stagas/dynamic-cache/blob/5e8db09ee99c5bf4074e23bc2ad2734c07be9803/src/index.ts#L58-L62 "Source code on GitHub")

Creates a DynamicCache.

```ts
const headers = new Headers({ 'Content-Type': 'text/plain' })
const cache = new DynamicCache('text', headers)
```

#### Parameters

* `namespace` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Namespace of the files in this cache
* `headers` **[Headers](https://developer.mozilla.org/docs/Web/HTML/Element/header)** Headers to use for this cache files

### put

[src/index.ts:77-84](https://github.com/stagas/dynamic-cache/blob/5e8db09ee99c5bf4074e23bc2ad2734c07be9803/src/index.ts#L77-L84 "Source code on GitHub")

Puts a file in the cache.

```ts
const url = await cache.put('foo', 'bar')
await fetch(url).then(res => res.text()) // => "bar"
```

#### Parameters

* `filename` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The filename of the resource
* `content` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The content of the resource
* `headers` Headers to be used (optional, default `this.headers`)

Returns **any** The full path of the resource, including the cache's prefix

### register

[src/index.ts:21-41](https://github.com/stagas/dynamic-cache/blob/5e8db09ee99c5bf4074e23bc2ad2734c07be9803/src/index.ts#L21-L41 "Source code on GitHub")

Registers the service worker.

```ts
// note you must serve `sw.js` at a higher level than your current url location
await DynamicCache.register('/sw.js')
```

#### Parameters

* `url` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The url of the service worker file (can find it under `dist/cjs/sw.js` or `dist/esm/sw.js`)
* `options` **RegistrationOptions** Service worker registration options (@see ) (optional, default `{scope:'/'}`)

## Contribute

[Fork](https://github.com/stagas/dynamic-cache/fork) or
[edit](https://github.dev/stagas/dynamic-cache) and submit a PR.

All contributions are welcome!

## License

MIT © 2021
[stagas](https://github.com/stagas)