Cached Middleware Fetch Demo
View Code ↗
cachedFetch() Duration
1295.52ms
API Response Timestamp
2025-09-04T11:54:33.732Z
Random Value (to verify cache)
0.651390
Current Time
2025-09-04T11:54:33.774Z
How it works:
- The middleware fetches from an API endpoint that has a 1000ms delay
- Uses SWR (Stale-While-Revalidate) caching strategy with separate revalidate and expiry times
- Cache HIT: Fresh cached data served instantly (~0-5ms)
- Cache STALE: Cached data served instantly, background refresh triggered via waitUntil() (~0-5ms)
- Cache MISS: No cached data available, full API delay incurred (~1000ms)
- Data is fresh for 5 seconds (revalidate time)
- After 5 seconds, data becomes stale but is served instantly while refreshing in background
- After 30 seconds, cache expires completely and requires a fresh fetch
- The random value helps verify when the cache is actually being used
- Cache status provided via response headers: X-Cache-Status, X-Cache-Age, X-Cache-Expires-In