What is fresh?
The 'fresh' npm package is a HTTP response freshness testing library. It is used to determine whether the response is still 'fresh' in the context of HTTP caching. It checks the request and response headers to decide if the response should be re-used or if a new one needs to be generated.
What are fresh's main functionalities?
Freshness Checking
This feature allows you to check if the HTTP response is still fresh by comparing the request and response headers. If the function returns true, the response is considered fresh.
const fresh = require('fresh');
const reqHeaders = { 'if-none-match': 'some-etag' };
const resHeaders = { 'etag': 'some-etag' };
const isFresh = fresh(reqHeaders, resHeaders);
console.log(isFresh); // true or false
Other packages similar to fresh
etag
The 'etag' package is used to generate HTTP ETags, which are typically used in HTTP headers to determine change in content at a given URL. While 'fresh' is used to check the freshness of responses, 'etag' helps in generating the identifiers that 'fresh' would use for its comparisons.
cache-control
The 'cache-control' package is used for parsing and formatting HTTP 'Cache-Control' headers. Unlike 'fresh', which checks for response freshness, 'cache-control' is more focused on providing utilities for working with the 'Cache-Control' header directly.
fresh
HTTP response freshness testing
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install fresh
API
var fresh = require('fresh')
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 recieve 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 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
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"bar"' }
fresh(reqHeaders, resHeaders)
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"foo"' }
fresh(reqHeaders, resHeaders)
Using with Node.js http server
var fresh = require('fresh')
var http = require('http')
var server = http.createServer(function (req, res) {
if (isFresh(req, res)) {
res.statusCode = 304
res.end()
return
}
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