10LOC

#cache-api

Web Platformadvanced

The Cache API for direct HTTP response caching

const CACHE_NAME = "api-cache-v1";

export const fetchWithCache = async (url: string, maxAgeMs: number) => {
  const cache = await caches.open(CACHE_NAME);
  const cached = await cache.match(url);

Cache real Response objects by URL from plain page scripts, with a manual max-age check, no service worker required.

Web Platformadvanced

A minimal cache-first offline service worker

const CACHE_NAME = "app-shell-v1";
const PRECACHE_URLS = ["/", "/styles.css", "/app.js", "/offline.html"];

self.addEventListener("install", (event) => {
  (event as Event & { waitUntil: (p: Promise<unknown>) => void }).waitUntil(

Precache the app shell on install and serve every request from cache first, falling back to the network only on a cache miss.