# @robireton/fresh

> HTTP response freshness testing

Latest version **1.0.0** (published 2023-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @robireton/fresh
pnpm add @robireton/fresh
yarn add @robireton/fresh
bun add @robireton/fresh
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-01-25 |
| First published | 2023-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Rob Ireton |
| Maintainers | robireton |
| Keywords | fresh, http, conditional, cache |

## Links

- npm: https://www.npmjs.com/package/@robireton/fresh
- Repository: https://github.com/robireton/fresh
- Homepage: https://github.com/robireton/fresh#readme
- Issues: https://github.com/robireton/fresh/issues
- npm.io page: https://npm.io/package/@robireton/fresh

## 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

- 1.0.0 (latest) — 2023-01-25

## README

# fresh

HTTP response freshness testing

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```shell
$ npm install @robireton/fresh
```

## API

```js
import fresh from '@robireton/fresh'
```

### fresh(reqHeaders, resHeaders)

Check freshness of the response using request and response headers.

When the response is still “fresh” in the client’s cache `true` is
returned, otherwise `false` is returned to indicate that the client
cache is now stale and the full response should be sent.

When a client sends the `Cache-Control: no-cache` request header to
indicate an end-to-end reload request, this module will return `false`
to make handling these requests transparent.

## Known Issues

This module is designed to only follow the HTTP specifications, not
to work-around all kinda of client bugs (especially since this module
typically does not receive enough information to understand what the
client actually is).

There is a known issue that in certain versions of Safari, Safari
will incorrectly make a request that allows this module to validate
freshness of the resource even when Safari does not have a
representation of the resource in the cache. The module
[jumanji](https://www.npmjs.com/package/jumanji) can be used in
an Express application to work-around this issue and also provides
links to further reading on this Safari bug.

## Example

### API usage

```js
const reqHeaders = { 'if-none-match': '"foo"' }
const resHeaders = { 'etag': '"bar"' }
fresh(reqHeaders, resHeaders)
// => false

const reqHeaders = { 'if-none-match': '"foo"' }
const resHeaders = { 'etag': '"foo"' }
fresh(reqHeaders, resHeaders)
// => true
```

### Using with Node.js http server

```js
import fresh from '@robireton/fresh'
import http from 'node:http'

const server = http.createServer((req, res) => {
  // perform server logic
  // ... including adding ETag / Last-Modified response headers

  if (isFresh(req, res)) {
    // client has a fresh copy of resource
    res.statusCode = 304
    res.end()
    return
  }

  // send the resource
  res.statusCode = 200
  res.end('hello, world!')
})

function isFresh (req, res) {
  return fresh(req.headers, {
    'etag': res.getHeader('ETag'),
    'last-modified': res.getHeader('Last-Modified')
  })
}

server.listen(3000)
```

## License

[MIT](LICENSE)

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