CacheStorage: named cache namespaces
Quick answer
CacheStorage manages named caches for storing Request/Response pairs. This is an explicit application store, separate from ordinary HTTP cache behavior.
How it works
On a supported secure origin, caches.open(name) creates or opens a Cache. caches.keys() lists cache names and caches.delete(name) removes a named cache. Use an application-specific prefix and version; the examples generate unique names and remove only their own cache in finally. Storage is subject to quota and eviction. Cache Storage is not a secure place for sensitive credentials.
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 cache=await caches.open(name);console.log(typeof cache.match,(await caches.keys()).includes(name));}finally{await caches.delete(name);}})()Expected console output (not executed locally):
function trueThe opened name becomes visible in CacheStorage.
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{await caches.open(name);console.log(await caches.delete(name),(await caches.keys()).includes(name));}finally{await caches.delete(name);}})()Expected console output (not executed locally):
true falseDeletion succeeds and the name no longer appears.
Common mistakes
Do not assume CacheStorage automatically applies HTTP freshness rules or that a cache persists forever. Never clear all origin caches during a lesson.
Practice
Create a uniquely named lab cache, inspect its presence in keys, then remove it and confirm the result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CacheStorage: named cache namespaces — Knowledge Check
Apply the concept and reason about local example output.
What does caches.open(name) resolve to?
Does CacheStorage guarantee indefinite persistence?
Why use an app-specific cache prefix?
What is the expected console output of CacheStorage: named cache namespaces example 1?
(async () => {const name="bm-v14-"+crypto.randomUUID();try{const cache=await caches.open(name);console.log(typeof cache.match,(await caches.keys()).includes(name));}finally{await caches.delete(name);}})()What is the expected console output of CacheStorage: named cache namespaces example 2?
(async () => {const name="bm-v14-"+crypto.randomUUID();try{await caches.open(name);console.log(await caches.delete(name),(await caches.keys()).includes(name));}finally{await caches.delete(name);}})()