0.0.8 • Published 7 years ago

sw-cacheable-response-behavior v0.0.8

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
7 years ago

sw-cacheable-response-behavior

This library takes a Response object and determines whether it's cacheable, based on a specific configuration.

Installation

npm install --save-dev sw-cacheable-response-behavior

Demo

Browse sample source code in the demo directory, or try it out directly.

API

sw-cacheable-response-behavior

packages/sw-cacheable-response-behavior/src/index.js:21-21

sw-cacheable-response-behavior Module

Behavior

packages/sw-cacheable-response-behavior/src/lib/behavior.js:48-119

Examples

// Used as an automatically invoked as "behavior" by a RequestWrapper:

const cacheName = 'runtime-cache';
const requestWrapper = new goog.runtimeCaching.RequestWrapper({
  cacheName,
  behaviors: [
    new goog.cacheableResponse.Behavior({
      statuses: [0, 200, 404]
    })
  ]
});

// Set up a route to match any requests made against the example.com domain.
// The responses will be cached if the response code is 0, 200, or 404, and
// will not be cached otherwise.
const route = new goog.routing.RegExpRoute({
  match: ({url}) => url.domain === 'example.com',
  handler: new goog.runtimeCaching.StaleWhileRevalidate({requestWrapper})
});
// Explicitly invoked usage independent of the goog.routing framework, via
// the isResponseCacheable() method:

// TODO: Write sample code.

constructor

packages/sw-cacheable-response-behavior/src/lib/behavior.js:62-73

Creates a new Behavior instance, which stores configuration and logic to determine whether a Response object is cacheable or not.

If multiple criteria are present (e.g. both statuses and headers), then the Response needs to meet all of the criteria to be cacheable.

Parameters

  • input Object The input object to this function.
    • input.statuses [Array<Number>] The status codes that are checked when determining whether a Response is cacheable.
    • input.headers [Object<String, String>] The header values that are checked when determining whether a Response is cacheable.
  • $0 Object (optional, default {})
    • $0.statuses
    • $0.headers