npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swork-cache

v1.0.1

Published

Caching implementation using the swork framework.

Downloads

1

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.