New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

swork-cache

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swork-cache

Caching implementation using the swork framework.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 08 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc