skip to content

Caching & Offline-First

A client cache picks cache-first, network-first or stale-while-revalidate per resource and queues offline writes for replay. Offline prompts test how you reason about staleness and conflicts.

on this pageshow

questions

6

For a gradebook web app on patchy school Wi-Fi, which caching strategy should its timetable, rosters, photos, grade history and parent messages each get?

level: middleimportance: must knowfreq 58%

answer

  1. staleness, privacy, can the lesson go on
  2. fresh roster, but never a blocked register
  3. a versioned photo URL never goes stale
  4. parent messages never touch the disk
  5. the key carries teacher, term and class

basics

~20 s

Argue each resource from tolerable staleness, privacy and whether the lesson can go on without it: timetable and grade history stale-while-revalidate, rosters network-first with a short timeout, photos cache-first on versioned URLs, parent messages network-only and never stored.

solid answer

~50 s

I'd write one policy per resource from three questions: how stale may it be, is it private, and can the lesson go on without it. Today's timetable is fetched at sign-in and served **stale-while-revalidate** with an "as of" time, because a room change a few hours late is tolerable. Rosters get **network-first with a two- to three-second timeout**, falling back to the cached copy marked with its age: a new pupil matters, but the register can't wait on flaky Wi-Fi. Photos are **cache-first** on URLs that change when the photo changes, so they never need revalidating. Grade history is **stale-while-revalidate** for reading. Parent messages are **network-only**, sent with `Cache-Control: no-store` and never written to IndexedDB or Cache Storage. Keys carry the teacher, plus the term and class where they apply, so a write, a new term or another teacher invalidates exactly what it should.

code

json · 7 lines
json
{
  "timetable":     { "strategy": "stale-while-revalidate", "prefetch": "at-sign-in", "key": ["teacherId", "date"] },
  "roster":        { "strategy": "network-first", "timeoutMs": 2500, "fallback": "cached-with-age", "key": ["teacherId", "termId", "classId"] },
  "studentPhoto":  { "strategy": "cache-first", "key": ["teacherId", "photoUrlWithVersion"] },
  "gradeHistory":  { "strategy": "stale-while-revalidate", "key": ["teacherId", "termId", "classId"], "invalidateOn": ["own-write-confirmed", "term-change"] },
  "parentMessages": { "strategy": "network-only", "persist": false, "responseHeader": "Cache-Control: no-store" }
}

go deeper

for a junior

Recall the core strategy names and that the choice is made per resource, not once for the whole app.

for a middle

Argue each resource from staleness, privacy and need-offline, and explain why the roster needs a timeout rather than plain network-first on flaky Wi-Fi.

for a senior

Show the keys and invalidation triggers, the age stamps the user sees, and how the table doubles as the shared-laptop wipe list.

for a principal

Treat the table as a contract other teams extend: who approves a new resource's strategy, and which tiers may never persist on a shared device.

## Three questions decide each resource A **caching strategy** is the rule a client follows when the app asks for a resource: answer from a local copy, from the network, or from one and then the other. The platform's guidance names several, among them **cache only**, **network only**, **cache first falling back to network**, **network first falling back to cache**, and **stale-while-revalidate** (serve the cached copy now, fetch an update for next time). How each one runs inside a service worker is the worker-caching question's subject; the design question is which one each *product* resource earns. For a school gradebook used on shared classroom laptops and teachers' phones over patchy Wi-Fi, three questions decide it: 1. **How stale may it be?** Minutes, hours, or not at all. 2. **Is it private?** Data about pupils and parents must not outlive the teacher's session on a shared machine. 3. **Can the lesson go on without it?** If the register can't open, the lesson stalls; if a report is unavailable, nobody notices until tomorrow. ## The policy table | Resource | Tolerable staleness | Private? | Needed offline? | Strategy | Freshness signal | |---|---|---|---|---|---| | Today's timetable | Hours | Low | Yes | Stale-while-revalidate, prefetched at sign-in | "As of 07:58" stamp | | Class rosters | Minutes | Yes | Yes, to take attendance | Network-first, 2-3 s timeout, cached fallback | Age shown on fallback | | Student photos | Never stale (versioned URL) | Yes | Helpful, for cover teachers | Cache-first | New URL when the photo changes | | Grade history | Hours, for reading | Yes | Useful | Stale-while-revalidate | Refreshed in the background | | Parent messages | None | Highly | No | Network-only, `no-store` | Always live | The app shell itself is precached with each release; how that cache is versioned is a separate question, and it holds nothing personal. ## Why the roster is neither cache-first nor plain network-first - **Cache-first** opens the register instantly but shows yesterday's class list: a pupil who joined this morning is missing, and nothing tells the teacher. - **Plain network-first** is correct online, but on an intermittent connection it waits for the request to fail before using the perfectly good copy on the device, which the guidance itself calls a frustrating experience. On school Wi-Fi that can take many seconds. - **Network-first with a short timeout** gives the fresh roster whenever the network answers in time, and the cached one, labelled with its age, when it doesn't. The lesson goes on either way. - **Stale-while-revalidate** is defensible too, but it answers this request from the cache; the fresh roster lands only on the next read unless the page re-renders on it, and re-rendering a register under the teacher's finger is its own problem. ## Keys and what invalidates them A cache entry's **key** decides who may read it and what makes it wrong: - **Teacher id** in every key, so a different signed-in teacher can never read a previous teacher's rosters, even before the sign-out wipe runs. - **Term id** in roster and grade keys: a new term changes the key, and the old term's entries are deleted rather than revalidated. - **Class id**, so a write invalidates one class, not the whole gradebook. - **A write** the teacher makes replaces or refetches the affected class's grade entry once the server confirms it. - **Photo version** in the URL itself, so a replaced photo is a new resource and the old one ages out. ## Two layers, one name The worker strategy called stale-while-revalidate and HTTP's `stale-while-revalidate` directive express one idea at two layers: the first is code the app ships, the second is a `Cache-Control` extension a server sends to caches. Keep them apart in the round: the policy table above is the app's decision, and the headers each response carries are the API's. The one header the design depends on is `no-store` on the parent-messages response, which keeps that page out of the browser's HTTP cache as well. ## What the policy costs - **More code paths**: five strategies mean five behaviours to test offline, online and on a flaky link. - **Visible staleness**: every cached read needs an age stamp, or the teacher cannot tell an old roster from a fresh one. - **Storage on shared machines**: everything persisted here must be wiped at sign-out, so the table doubles as the wipe list. - **A deliberate gap**: parent messages do not work offline, and the interface must say so rather than fail silently.

  • A roster refresh arrives mid-register with one pupil added. What should the screen do?
    Keep every mark already taken, because marks are keyed by pupil id, not by row position. Insert the new pupil with a visible "new" marker or a short notice ("Roster updated: 1 pupil added") rather than silently reshuffling the list under the teacher's finger. If a pupil was removed, keep any mark already taken for them and flag it, so the server can decide what to do with it.
  • Why prefetch the timetable and rosters at sign-in instead of caching them as they are opened?
    Because the moment a teacher needs them offline is often the first time that day they open them: a cover lesson in a room with no signal. Prefetching today's timetable and the rosters it references, while the staffroom Wi-Fi works, turns "cached if visited" into "available for today's lessons". The cost is data on the device that the sign-out wipe must remove.
  • Would you ever make grade history network-first?
    Yes, on the screen where a teacher is about to change a grade, because editing against an hours-old copy invites a conflict with a colleague's edit. Reading history can stay stale-while-revalidate; the edit form fetches the current value and its revision first, and falls back to the cached copy only with a warning that the grade may have changed.

  • One strategy for every API response, usually network-first.
  • Make the roster cache-first so the register always opens instantly.
  • Plain network-first is fine on school Wi-Fi; it falls back eventually.
  • Keep parent messages offline too, since the page sits behind a login.
  • Cache keys only need the URL; which teacher is signed in doesn't matter.
open as a page

In a gradebook web app open in several tabs, how would you design the offline write queue's ordering and idempotent replay, and which tab replays it?

level: seniorimportance: must knowfreq 44%

basics

~20 s

Store each write durably before showing it, ordered per target, carrying an idempotency key minted at creation; one context replays under a named Web Lock, triggered from the page whether or not Background Sync exists.

open as a page

When a gradebook web app loses school Wi-Fi mid-lesson, what should the teacher see, and which actions should stay available or be disabled?

level: middleimportance: should knowfreq 47%

basics

~20 s

Keep the lesson going: attendance and grades still save, each marked as not yet sent; cached rosters show their age; only live-only features such as parent messages are disabled with a reason. Treat navigator.onLine as a hint, never a gate.

open as a page

In a gradebook web app, a grade entered offline replays after a colleague changed that grade; how should the server reject it, and what should the teacher see?

level: seniorimportance: should knowfreq 36%

basics

~20 s

The queued grade carries the revision the teacher saw; the server refuses it once the record has moved on, returning the current value. The app parks that entry and shows both versions: keep theirs, apply mine, or edit.

open as a page

When a teacher signs out of a gradebook web app on a shared classroom laptop, what must be wiped, and how do you make sure it happened?

level: seniorimportance: should knowfreq 33%

basics

~20 s

Every copy of the teacher's data: IndexedDB, the worker's caches, Web Storage, the HTTP cache and every tab's memory. Settle unsent work first, wipe locally without the network, send Clear-Site-Data as a backstop, and test it.

open as a page

As frontend lead for a school district's gradebook, timetable and parent-portal web apps on shared classroom laptops, what offline-data policy would you set, and who owns it?

level: principalimportance: should knowfreq 24%

basics

~20 s

Classify data into tiers fixing where each may live and when it is wiped, assume shared devices, and give one team the shared queue and wipe. The data-protection owner owns the tiers; app teams declare resources against them.

open as a page