caches
object that provides a group of methods for cache-related operations is injected.caches.default
to fetch the default cache instance.// Fetch the default cache instance.const cache = caches.default;// This method provides the same effect as `caches.default`.await caches.open('default');
caches.open
to create a cache instance with the specified namespace.// Create a cache instance with the specified namespace.const cache = await caches.open(namespace);
caches.open(namespace)
method.Parameter | Type | Required | Description |
namespace | string | Yes | The namespace of the cache. The value "default" specifies the default instance. You can also use caches.default to fetch a default instance. |
cache.match(request: string | Request, options?: MatchOptions): Promise<Response | undefined>
Parameter | Type | Required | Description |
request | string | Request | Yes | The request object. The following method and headers are supported: GET Only the GET method is supported. If this parameter is set to a string, the string is used as a URL to construct a Request object. Range If the request contains a Range header and the cached response contains the Accept-Ranges header, the 206 response is returned. If-Modified-Since If the request contains a If-Modified-Since header and the cached response contains a Last-Modified header with the same value as that of the If-Modified-Since header, the 304 response is returned. If-None-Match If the request contains a If-None-Match header and the cached response contains an ETag header with the same value as that of the If-Modified-Since header, the 304 response is returned. |
options | No | The options. |
Parameter | Type | Example | Description |
ignoreMethod | boolean | true | Specifies whether to ignore the request method. If you set this parameter to true, the request is considered to be a GET request regardless of its actual method. |
cache.put(request: string | Request, response: Response): Promise<undefined>
Promise<undefined>
object is returned regardless of whether caching is successful.Parameter | Type | Required | Description |
request | string | Request | Yes | The cache key. GET The request parameter supports only the GET method. If other methods are specified, an error is thrown. string If the request parameter is set to a string, the string is used as a URL to construct a Request object. |
response | Yes | The cache content. Cache-Control Valid values: s-maxage, max-age, no-store, no-cache, and private. The values no-store, no-cache, and private indicate no caching. If you use these values, the cache.put method returns a 413 error. Pragma ETag If the request parameter of the cache.match method contains a If-None-Match header, you can associate it with ETag. Last-Modified if the request parameter of the cache.match method contains a If-Modified-Since header, you can associate it with Last-Modified. 416 Range Not Satisfiable If the object specified in the response parameter is 416 Range Not Satisfiable, no caching is performed. |
cache.put
method throws an error in the following scenarios:request
parameter specifies a method other than the GET method.response
parameter is 206 Partial Content. response
parameter contains a Vary: * header.cache.delete(request: string | Request, options?: DeleteOptions): Promise<boolean>
true
is returned. Otherwise, a Promise containing false
is returned.Parameter | Type | Example | Description |
ignoreMethod | boolean | true | Specifies whether to ignore the request method. If you set this parameter to true, the request is considered to be a GET request regardless of its actual method. |
Was this page helpful?