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

serviceberry-cache-control

v0.2.3

Published

A Cache Control plugin for Serviceberry

Downloads

122

Readme

serviceberry-cache-control

CircleCI Test Coverage Maintainability npm version

Cache control plugin for Serviceberry.

Install

npm install serviceberry-cache-control

Usage

This plugin exports an abstract class CacheControl for extending by your class that knows how to get ETags and Last Modified for the requested resource. To use this plugin extend CacheControl and implement at least getETag(request, response) or getLastModified(request, response). The ETag or Last Modified is then used to validate the client cache using information passed in request headers.

This plugin sets a Cache-Control response header describing how the response should be cached based on the plugin options and halts the request and responds with a 304 Not Modified status if the cache validates using the ETag or Last Modified as described above.

const CacheControl = require("serviceberry-cache-control");

class Caching extends CacheControl {
	getETag (request) {
		return data.getETag(request.getUrl()); // can also return a promise or use async/await
	}
}

trunk.use(new Caching(options));

Options

  • noStore boolean

    When true the no-store directive is set in the Cache-Control response header. Defaults to false.

  • noCache boolean

    When true the no-cache directive is set in the Cache-Control response header. Defaults to false.

  • mustRevalidate boolean

    When true the must-revalidate directive is set in the Cache-Control response header. Defaults to false.

  • public boolean

    When true the public directive is set in the Cache-Control response header. Defaults to false.

  • private boolean

    When true the private directive is set in the Cache-Control response header. Defaults to false.

  • maxAge number

    When greater than 0 the max-age directive is set in the Cache-Control response header. Defaults to NaN.

  • vary array

    An array of header field names that might may vary the response and should be considered by caches. Values are appended to the Vary response header.

    Defaults to [].

CacheControl

Abstract class

constructor([options])

  • options

    Sets this.options. See options above.

getETag(request, response)

You should extend this class and at least implement this method or getLastModified().

Called by the setETag method for fetching an ETag to be set as a response headers and used to validate the cache. This can be an async function or it can return a promise. It should return an ETag string or eventually resolve to one.

getLastModified(request, response)

You should extend this class and at least implement this method or getETag().

Called by the validate method for fetching the date the requested resource was last modified to be used to validate the cache. This can be an async function or it can return a promise. It should return an ETag string or eventually resolve to one.

use(request, response)

The handler method. This is the method called by Serviceberry. This is an async function. If it determines the response status should be 304 Not Modified the request will be halted and the response sent with a 304 status.

validate(request, response)

Called by the use method to validate the cache. This is an async function.