JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 29775365
  • Score
    100M100P100Q241322F
  • License BSD-2-Clause

Parses Cache-Control headers and friends

Package Exports

  • http-cache-semantics

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http-cache-semantics) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

HTTP cache semantics

CachePolicy object that computes properties of a HTTP response, such as whether it's fresh or stale, and how long it can be cached for. Based on RFC 7234.

Usage

const cache = new CachePolicy(request, response, options);

// Age counts from the time response has been created
const secondsFresh = cache.maxAge();
const secondsOld = cache.age();

// Current state
const outOfDate = cache.stale();

Cacheability of response depends on how it was requested, so both request and response are required. Both are objects with headers property that is an object with lowercased header names as keys, e.g.

const request = {
    method: 'GET',
    headers: {
        'accept': '*/*',
    },
};

const response = {
    status: 200,
    headers: {
        'cache-control': 'public, max-age=7234',
    },
};

const options = {
    shared: true,
};

If options.shared is true (default), then response is evaluated from perspective of a shared cache (i.e. private is not cacheable and s-maxage is respected). If options.shared is false, then response is evaluated from perspective of a single-user cache (i.e. private is cacheable and s-maxage is ignored).

stale()

Returns true if the response is stale (i.e. not fresh).

It generally means the response can't be used any more without revalidation with the server. However, there are exceptions, e.g. client can explicitly allow stale responses. A fresh response still may not be used if other conditions—such as Vary—are not satisfied.

cacheKey()

Returns a string that is a combination of method, URL, and headers selected with Vary.

Note that Vary: * never matches any request, so matching of cache keys alone is not sufficient to satisfy a request.

Implemented

  • Expires with check for bad clocks
  • Cache-Control response header
  • Pragma response header
  • Age response header
  • Default cacheability of statuses and methods
  • Basic support for Vary

Unimplemented

  • No support for revalidation and stale responses