Course contentsShow
← Web Development
Lesson 2419 of 2,98243. Service Workers Deep DiveFree lesson

Runtime Caching Fundamentals

Understand runtime caching: caching resources on-demand as users navigate, not at install time.

Runtime Caching Fundamentals

What you'll learn: How to cache resources on-demand as users navigate your app, rather than at install or activation time.

The Core Difference

By now you've learned about precaching, where you define a manifest of assets to cache when the service worker installs. Runtime caching is different—it caches resources dynamically as users request them during normal app usage.

Think of precaching like stocking your pantry with staples before a storm. Runtime caching is like grabbing ingredients you need while cooking—you only cache what's actually requested.

How It Works

When a user navigates your app and requests a resource (an API response, an image, a page you didn't precache), your service worker intercepts that fetch event. Instead of just passing the request through, you can:

  1. Check if it's in the cache
  2. If not, fetch it from the network
  3. Store a copy in the cache for next time
  4. Return the response

This happens on-demand, resource by resource, as users explore your app.

Why Runtime Caching Matters

Not everything can or should be precached. User-generated content, API responses, third-party resources, or infrequently accessed pages are perfect candidates for runtime caching. You don't know what every user will need upfront, so you cache intelligently as they go.

This approach lets you build a personalized cache tailored to each user's behavior, improving performance on repeat visits without bloating the initial precache.

Key Takeaway: Runtime caching captures resources on-the-fly as users navigate, building a dynamic cache based on actual usage patterns rather than predetermined asset lists.