2.0.0 • Published 7 years ago

koa-cache-control v2.0.0

Weekly downloads
4,149
License
MIT
Repository
github
Last release
7 years ago

koa-cache-control

A simple method for managing cache control headers from your application. It also tries to provide a simple set of rules for common use cases such as setting 'max-age=0' when 'no-cache' is present by default.

Example

Configuring noCache easily:

app.use(cacheControl({
    noCache: true
}));

Creates a cache-control header of no-cache, max-age=0

Usage

To start using cacheControl, just use the middleware in your application:

app.use(cacheControl());

Default Cache Headers

When initialising the middleware you can set default options when you use it in your application:

app.use(cacheControl({
    maxAge: 5
}));

Overriding Defaults

Just set the cacheControl object after the cacheControl() middleware is loaded on the request context:

app.use(function *(next){
    this.cacheControl = {
        maxAge: 60
    };

    yield next;
});

This is useful in error conditions where you can setup cache headers before and after a request is processed:

app.use(function *(next){
    this.cacheControl = {
        maxAge: 60
    };

    try {
        yield next;
    } catch (err) {
        this.cacheControl = {
            maxAge: 5
        };
    }
});

Options

NameValueDescription
privateBooleanAdds 'private' flag, overrides 'public' option
publicBooleanAdds 'public' flag
noStoreBooleanAdds 'no-store' flag and includes noCache
noCacheBooleanAdds 'no-cache' flag, sets maxAge to 0 and removes sMaxAge, staleIfError and staleWhileRevalidate
noTransformBooleanAdds 'no-transform' flag
mustRevalidateBooleanAdds 'must-revalidate' flag and removes staleIfError and staleWhileRevalidate
staleIfErrorNumberAdds 'stale-if-error=%d' flag
staleWhileRevalidateNumberAdds 'stale-while-revalidate=%d' flag
maxAgeNumberAdds 'max-age=%d' flag
sMaxAgeNumberAdds 's-maxage=%d' flag