main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | # CacheStorage
|
---|
2 |
|
---|
3 | Undici exposes a W3C spec-compliant implementation of [CacheStorage](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) and [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
---|
4 |
|
---|
5 | ## Opening a Cache
|
---|
6 |
|
---|
7 | Undici exports a top-level CacheStorage instance. You can open a new Cache, or duplicate a Cache with an existing name, by using `CacheStorage.prototype.open`. If you open a Cache with the same name as an already-existing Cache, its list of cached Responses will be shared between both instances.
|
---|
8 |
|
---|
9 | ```mjs
|
---|
10 | import { caches } from 'undici'
|
---|
11 |
|
---|
12 | const cache_1 = await caches.open('v1')
|
---|
13 | const cache_2 = await caches.open('v1')
|
---|
14 |
|
---|
15 | // Although .open() creates a new instance,
|
---|
16 | assert(cache_1 !== cache_2)
|
---|
17 | // The same Response is matched in both.
|
---|
18 | assert.deepStrictEqual(await cache_1.match('/req'), await cache_2.match('/req'))
|
---|
19 | ```
|
---|
20 |
|
---|
21 | ## Deleting a Cache
|
---|
22 |
|
---|
23 | If a Cache is deleted, the cached Responses/Requests can still be used.
|
---|
24 |
|
---|
25 | ```mjs
|
---|
26 | const response = await cache_1.match('/req')
|
---|
27 | await caches.delete('v1')
|
---|
28 |
|
---|
29 | await response.text() // the Response's body
|
---|
30 | ```
|
---|
Note:
See
TracBrowser
for help on using the repository browser.