makestatic-http-cache
v1.1.10
Published
Caching HTTP client
Downloads
4
Maintainers
Readme
HTTP Cache
Caches HTTP requests
Caches HTTP requests to disc for faster builds.
Install
yarn add makestatic-http-cache
API
HttpCache
Caches HTTP requests to disc so that plugins that make network requests are faster on consecutive builds.
This plugin is automatically available when using the core-standard
library.
See Also
HttpCache
new HttpCache(context, options)
Create an HttpCache plugin.
Configure this plugin for the build
phase.
Assigns this plugin instance to context.agent
and does nothing
else until the public functions are invoked by other plugins.
If no directory
is given files are cached in the cache
folder within
this package.
This implementation will only cache responses for the 200 status code,
you can cache responses for other status codes using the codes
option.
To disable caching you can set the expires
option to zero. If the
context option cache
has been set to false
the expires
value is
forced to zero and the cache is bypassed.
This implementation respects the ETag header and will only use a cached resource when the ETag has not changed.
If a server sends a Cache-Control
header containing no-store
the
response is not cached.
If a server sends a Cache-Control
header with a max-age
value it is
respected and overrides the expires
option.
If a Cache-Control
header does not contain the no-store
value and the
server response has not expired the result is served from the cache with
no network request (optimal path).
For each request a directory is created corresponding to the request URL
in the cache directory
. Within each directory a meta.json
file is
written containing information about the last request, for methods other
than HEAD
a body
file is also written to the directory containing the
response body buffer.
context
Object the processing context.options
Object plugin options.
Options
directory
String the directory used to cache requests.expires
Number=86400 number of seconds before a cache expires.codes
Array list of HTTP status codes that are cacheable.
.request
HttpCache.prototype.request(url, options[, cb])
Perform a request and cache the result.
If a result already exists in the cache and is not stale the cached result is returned.
Options are passed to the http.request()
or https.request()
functions
depending upon the URL, see the relevant nodejs documentation for more
information. Options related to the URL are automatically set by calling
require('url').parse(url)
so you do not need to assign those options.
If the buffer
option is set to false
then the response body data is
not read which is useful if you either just want the headers for a method
other than HEAD or if you are reading in the body
data when consuming
the response (for example to calculate checksums).
The response object that the promise resolves to will contain meta
information about the last request and a body
field which is the buffer
content from the last request.
The field hit
indicates whether the result is coming from the cache or
from a network request. It will be false
when coming from the network.
When the cb
callback function is specified it is invoked with the server
response object.
Returns a promise that resolves to the downloaded or cached content.
url
String the URL to fetch.options
Object request options.cb
Function callback function.
.head
HttpCache.prototype.head(url[, options][, cb])
Perform a HEAD request and cache the result meta data only.
Returns a promise that resolves to the downloaded or cached content.
url
String the URL to fetch.options
Object request options.cb
Function callback function.
.get
HttpCache.prototype.get(url[, options][, cb])
Perform a GET request and cache the result meta data and response buffer.
Returns a promise that resolves to the downloaded or cached content.
url
String the URL to fetch.options
Object request options.cb
Function callback function.
.post
HttpCache.prototype.post(url[, options][, cb])
Perform a POST request and cache the result meta data and response buffer.
If you add a data
field to the options
it is sent as the POST data.
Returns a promise that resolves to the downloaded or cached content.
url
String the URL to fetch.options
Object request options.cb
Function callback function.
License
MIT
Created by mkdoc on March 13, 2017