# koa-cache-control

> Middleware for meddling with Cache-Control headers

Latest version **2.0.0** (published 2017-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-cache-control
pnpm add koa-cache-control
yarn add koa-cache-control
bun add koa-cache-control
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-02-25 |
| First published | 2015-10-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/koa-cache-control) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Christopher Sidebottom |
| Maintainers | damouse404 |
| Keywords | Koa, cache, cache-control, no-cache |

## Links

- npm: https://www.npmjs.com/package/koa-cache-control
- Repository: https://github.com/DaMouse404/koa-cache-control
- Homepage: https://github.com/DaMouse404/koa-cache-control#readme
- Issues: https://github.com/DaMouse404/koa-cache-headers/issues
- npm.io page: https://npm.io/package/koa-cache-control

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2017-02-25
- 2.0.0-alpha.2 — 2016-12-07
- 1.0.0 — 2015-10-07

## README

# 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:
```js
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:
```js
app.use(cacheControl());
```

### Default Cache Headers
When initialising the middleware you can set default options when you use it in your application:
```js
app.use(cacheControl({
    maxAge: 5
}));
```

### Overriding Defaults
Just set the cacheControl object after the cacheControl() middleware is loaded on the request context:
```js
app.use(function (ctx, next) {
    ctx.cacheControl = {
        maxAge: 60
    };

    return next();
});
```

This is useful in error conditions where you can setup cache headers before and after a request is processed:
```js
app.use(function (ctx, next) {
    ctx.cacheControl = {
        maxAge: 60
    };

    return next().catch((err) => {
      ctx.cacheControl = {
          maxAge: 5
      };
    });
});
```

## Options
Name | Value | Description
----|----|---
private | Boolean | Adds 'private' flag, overrides 'public' option
public | Boolean | Adds 'public' flag
noStore | Boolean | Adds 'no-store' flag and includes noCache
noCache | Boolean | Adds 'no-cache' flag, sets maxAge to 0 and removes sMaxAge, staleIfError and staleWhileRevalidate
noTransform | Boolean | Adds 'no-transform' flag
mustRevalidate | Boolean | Adds 'must-revalidate' flag and removes staleIfError and staleWhileRevalidate
staleIfError | Number | Adds 'stale-if-error=%d' flag
staleWhileRevalidate | Number | Adds 'stale-while-revalidate=%d' flag
maxAge | Number | Adds 'max-age=%d' flag
sMaxAge | Number | Adds 's-maxage=%d' flag

---
_Source: https://npm.io/package/koa-cache-control · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
