swork-cache

Caching implementation using the swork framework.

Usage no npm install needed!

<script type="module">
  import sworkCache from 'https://cdn.skypack.dev/swork-cache';
</script>

README

swork-cache

swork-cache is caching middleware for swork applications and is built with TypeScript and async methods.

Example

import { Swork } from "swork";
import { Router } from "swork-router";
import { strategies } from "swork-cache";

const app = new Swork();
const router = new Router();

router.get("/api/foos", strategies.backgroundFetch());

app.use(router.routes());

app.listen();

Installation

Install via npm:

npm install swork-cache

Install via yarn:

yarn add swork-cache

Cache Strategies

backgroundFetch

This strategy will immediately return a value from the cache if it exists. If a cache entry is found, a fetch for the latest version of the asset will occur in the background and update the cache. If a cache entry is not found, the request will respond with the fetch of the asset immediately after updating the cache.

cacheFirst

This strategy will immediately return a value from the cache if it exists. If a cache entry is not found, the request will respond with the fetch of the asset immediately after updating the cache. Once an item is in the cache, there will be no subsequent requests to get the latest version.

networkFirst

This strategy will attempt to fetch the asset from the source and will pass on the response if there is no error. In the case of an error, the strategy will attempt to find a match in the cache.

networkOnly

This strategy only attempts to get the asset from the network source and does not utilize caching.

Custom Cache Strategies

All of the pre-defined cache strategies implement the CacheStrategy type. To provide a custom caching strategy, just define a method matching the CacheStrategy signature.

/**
 * Defines a cache strategy delegate. 'key' defaults to service worker version.
 */
type CacheStrategy = (cacheKey?: string) => (context: FetchContext, next: () => Promise<void>) => Promise<void> | void;

The cacheKey parameter is intended to specify a specific cache and defaults to the service worker version found in configuration.version provided by swork.

Cache Event Handlers

In addition to caching strategies, swork-cache provides event handlers to simplify management of cache entries.

preCache

preCache is an event handler that will cache any provided urls during the install phase of the service worker.

import { events } from "swork-cache";

// ...
app.on("install", events.install.preCache([
    // array of urls to pre-cache
]);

clearCacheOnUpdate

clearCacheOnUpdate is an event handler that will delete unnecessary cache entries during the activate phase of the service worker. Which cache entries to remove is determined by an overridable whitelist of keys that defaults to the configuration.version provided by swork.

import { events } from "swork-cache";

// ...
app.on("activate", events.activate.clearCacheOnUpdate());
// or
app.on("activate", events.activate.clearCacheOnUpdate({
    whitelist: ["api-data"],
    ignoreCase: true
}));

Contact

If you are using swork or any of its related middlewares, please let me know on gitter. I am always looking for feedback or additional middleware ideas.