Offline response policy and update compatibility
Quick answer
An offline strategy chooses whether to use the network, a stored response or a fallback. Choose policies based on resource freshness and sensitivity.
How it works
Cache-first can suit versioned public assets. Network-first can suit public data that should be fresh, with a clearly labeled stale fallback when transport fails. A network HTTP error is different from a rejected fetch; decide which errors warrant fallback. Do not silently serve private or obsolete data. Use an allowlist of public GET resources and versioned cache names. These examples model the selection policy with injected responses and local data, not actual Cache Storage, service worker interception or network outages.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {async function choose(network,cached){try{return await network();}catch{return cached??new Response("unavailable",{status:503});}}const r=await choose(()=>Promise.reject(new Error("simulated transport failure")),new Response("cached public data"));console.log(await r.text());})()Expected console output:
cached public dataThe injected transport rejects and the supplied cached response is chosen. No real cache or network is used.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const allow=new Set(["/assets/app.v2.js"]);const shouldCache=(method,path,ok)=>method==="GET"&&allow.has(path)&&ok;console.log(shouldCache("GET","/assets/app.v2.js",true),shouldCache("POST","/assets/app.v2.js",true),shouldCache("GET","/private",true));})()Expected console output:
true false falseOnly the permitted successful public GET is selected.
Common mistakes
Do not treat a cache hit as proof of freshness. Avoid deleting assets used by older clients before coordinating an update.
Practice
Compare a public GET request for a versioned asset with a POST submission. Design an offline fallback that explicitly identifies stale content.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Offline response policy and update compatibility — Knowledge Check
Apply the concept and reason about local example output.
Which resource is a natural cache-first candidate?
What should a stale offline fallback communicate?
Why coordinate asset cleanup with open clients?
What is the expected console output of Offline response policy and update compatibility example 1?
(async () => {async function choose(network,cached){try{return await network();}catch{return cached??new Response("unavailable",{status:503});}}const r=await choose(()=>Promise.reject(new Error("simulated transport failure")),new Response("cached public data"));console.log(await r.text());})()What is the expected console output of Offline response policy and update compatibility example 2?
(async () => {const allow=new Set(["/assets/app.v2.js"]);const shouldCache=(method,path,ok)=>method==="GET"&&allow.has(path)&&ok;console.log(shouldCache("GET","/assets/app.v2.js",true),shouldCache("POST","/assets/app.v2.js",true),shouldCache("GET","/private",true));})()