Skip to content
tezvyn:

Design a Service Worker caching strategy for a news app

Source: web.devHardHow cards are made

Design a Service Worker caching strategy for a news app
Tests

Matching resources to caching patterns and justifying speed vs freshness.

Key points

Cache First for the app shell, Stale-While-Revalidate for static assets, and Network First for article APIs.

Watch out for

One strategy everywhere or ignoring cache quotas.

What's really being asked

This question evaluates whether you understand that Service Worker caching is not one-size-fits-all. Interviewers want to see if you can categorize resources by their update frequency and criticality, select patterns that balance performance against freshness, and discuss practical constraints like storage quotas, cache expiration, and how the Service Worker layer interacts with the underlying HTTP cache. They also care whether you can justify trade-offs in user experience rather than simply naming patterns.

The full answer

Four decisions in order. First, the application shell should use Cache First because it is versioned, critical for render, and must load instantly even offline. Second, static assets like CSS and JS bundles should use Stale-While-Revalidate so returning visitors get sub-second loads while the browser fetches an updated version in the background for the next visit. Third, dynamic article API data should use Network First with a short TTL fallback cache so online users always see the latest headlines, but offline users still have something readable rather than a blank screen. Fourth, you must mention cache cleanup strategies such as limiting the number of entries or using a max-age, because mobile devices enforce storage quotas and uncapped growth will evict your origin.

The mistakes people make

Treating every resource with the same pattern, such as Cache First for APIs, which would serve days-old news as current. Another red flag is ignoring the HTTP cache entirely; a senior candidate should note that the Service Worker sits in front of the browser cache and can either bypass it with fetch options or let it absorb redundant requests. Proposing infinite cache growth without eviction logic is also a junior-level mistake. Finally, suggesting Network First for the app shell misses the point of instant offline rendering.

What usually comes next

How would you invalidate the app shell after a deployment? How do you handle opaque responses from third-party CDNs? What happens when the user exceeds the browser storage quota? How would you sync read markers or user preferences when connectivity returns?

A concrete example

In a news PWA, the Service Worker precaches the shell HTML and main bundle at the install event. On a page load, the shell is served from Cache First. A request for article.json is handled with Network First; if the network is down, the last successful response is returned from a cache named content-v1 that stores only the twenty most recent articles. CSS files use Stale-While-Revalidate with a one-day max-age in the Service Worker, while the HTTP cache layer is configured with immutable headers for hashed filenames to prevent revalidation round trips.

Interview question

In a news PWA, which resource-to-strategy mapping best balances offline resilience with content freshness?

  • a.Cache First for the app shell, Stale-While-Revalidate for CSS and JS bundles, and Network First for article APIsCorrect
  • b.Cache First for the app shell, Network First for CSS and JS bundles, and Cache First for article APIs
  • c.Stale-While-Revalidate for the app shell, Network First for CSS and JS bundles, and Cache First for article APIs
  • d.Network First for the app shell, Cache First for CSS and JS bundles, and Stale-While-Revalidate for article APIs
Why?

This mapping matches update frequency and criticality: the versioned app shell must render instantly offline, static assets can load immediately while refreshing in the background, and article APIs should try the network first to avoid serving stale headlines. Option B is tempting because it correctly pairs Cache First with the shell, but it wrongly applies Network First to static assets, which delays returning visitors, and Cache First to APIs, which would present old news as current.

Just read this? Test yourself on what you have been reading.

Read the original → web.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles