Cache responses in memory or sessionStorage to eliminate redundant network requests.

Basic Caching

Add data-cache to cache GET responses:

Duration Strings

ValueDuration
true or ""5 minutes (default)
30s30 seconds
5m5 minutes
1h1 hour
1d1 day

When a cached response is available, html★ skips the network request entirely and swaps the cached HTML.

Stale-While-Revalidate (SWR)

SWR serves cached content instantly, then revalidates in the background:

With SWR:

  1. Cache hit (fresh) — serves cached content, no fetch
  2. Cache hit (stale) — serves stale content immediately, fetches in background, swaps when response arrives
  3. Cache miss — fetches normally

Use data-cache="swr" for the default 5-minute TTL, or data-cache="swr:30s" for a custom duration.

Persistent Storage

By default, cache lives in memory and is lost on page refresh. Use data-cache-storage to persist to sessionStorage:

Session-backed cache survives page refreshes and lives for the browser tab's lifetime. Memory cache is checked first, with sessionStorage as a fallback.

Cache Invalidation

Use data-cache-invalidate on non-GET requests to clear cached URLs when data changes:

Multiple patterns (comma-separated):

Patterns are matched as regular expressions against cached URLs. Invalidation runs automatically when a non-GET request succeeds.

Cascade

data-cache inherits through the attribute cascade, so you can set it on a parent:

JavaScript API

Control the cache programmatically:

ModeBehavior
hoverPrefetch on mouseenter (default)
hover:<ms>Prefetch after hovering for specified milliseconds
visiblePrefetch when element scrolls into viewport
eagerPrefetch immediately on page load
falseDisable prefetching

Prefetched responses are stored in cache with the element's data-cache TTL (default 5 minutes). Only GET requests are prefetched. data-prefetch inherits through the cascade.

You can also prefetch programmatically:

Navigation responses are cached for 10 minutes in sessionStorage. Back/forward navigation feels instant.

Dashboard with SWR

Dashboard widgets show the last known data instantly, then quietly update when fresh data arrives.

Form with Cache Invalidation

Adding a todo invalidates the cached list, so the next "Refresh" click fetches fresh data.