jed / evented-cache-api

A drop-in wrapper for the Service Worker Cache API that adds cache/response events

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

evented-cache-api

Usage

This library is a drop-in wrapper around the browser's CacheStorage and Cache APIs. It adds the following events, a la StorageEvent:

  • cache: dispatched to the caches object when a cache is added or removed. the details property of the dispatched CustomEvent contains two properties: name for the name of the cache, and cache for its value (null when deleted).
  • response: dispatched to an individual cache when a response is updated. the details property of the dispatched CustomEvent contains two properties: request for the request updated, and response for its value (null when deleted).
import {caches} from './evented-cache-api.js'

void async function() {
  // start with a clean slate
  await caches.delete('test')

  // listen for a cache event when the cache is created
  caches.addEventListener('cache', e => console.log(e.detail))

  // the following logs {name: 'test', cache: [EventTarget]}
  let cache = await caches.open('test')

  // listen for a response event when the response is created
  cache.addEventListener('response', e => console.log(e.detail))

  // the following logs {request: './hello.html', response: [Response]}
  await cache.put('hello.html', new Response('world'))

  // the following logs {request: './hello.html', response: null}
  await cache.delete('hello.html')
}()

About

A drop-in wrapper for the Service Worker Cache API that adds cache/response events

License:MIT License


Languages

Language:JavaScript 65.4%Language:HTML 34.6%