Cache entries: put, match and response bodies
Quick answer
A Cache stores Request/Response pairs. Cache.put writes an eligible pair and Cache.match searches for a matching request.
How it works
Use a GET request with an HTTP(S) URL and an eligible Response. cache.put(request,response) consumes the supplied response body; clone the response first if it must also be returned to the caller. cache.match(request) resolves to a Response or undefined on miss. Matching includes URL and relevant Vary semantics unless explicitly overridden. cache.add fetches and stores a successful response; put itself does not require a successful HTTP status, so enforce your policy. Both examples use synthetic responses in a uniquely named local cache.
Local examples
Run in the isolated secure-origin offline-lab page; NOT RUN in local validation. See projects/offline-lab/README.md.
(async () => {const name="bm-v14-"+crypto.randomUUID();try{const c=await caches.open(name),url=new URL("v14-item",location.href).href,r=new Response("saved");await c.put(url,r.clone());console.log(await r.text(),await (await c.match(url)).text());}finally{await caches.delete(name);}})()Expected console output (not executed locally):
saved savedA clone is stored so the original body remains readable.
Run in the isolated secure-origin offline-lab page; NOT RUN in local validation. See projects/offline-lab/README.md.
(async () => {const name="bm-v14-"+crypto.randomUUID();try{const c=await caches.open(name);console.log(String(await c.match(new URL("v14-missing",location.href).href)));}finally{await caches.delete(name);}})()Expected console output (not executed locally):
undefinedA new empty cache contains no matching request.
Common mistakes
Do not cache a used response body, arbitrary personalized content or every status. Check cache misses before reading text.
Practice
Store a synthetic public response, match it, and read both the original response and the cached copy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cache entries: put, match and response bodies — Knowledge Check
Apply the concept and reason about local example output.
What does Cache.match resolve to on a miss?
Why clone a Response before caching and returning it?
Does Cache.put reject every non-2xx response?
What is the expected console output of Cache entries: put, match and response bodies example 1?
(async () => {const name="bm-v14-"+crypto.randomUUID();try{const c=await caches.open(name),url=new URL("v14-item",location.href).href,r=new Response("saved");await c.put(url,r.clone());console.log(await r.text(),await (await c.match(url)).text());}finally{await caches.delete(name);}})()What is the expected console output of Cache entries: put, match and response bodies example 2?
(async () => {const name="bm-v14-"+crypto.randomUUID();try{const c=await caches.open(name);console.log(String(await c.match(new URL("v14-missing",location.href).href)));}finally{await caches.delete(name);}})()