Stac widget and can be customized for different use cases.
How Caching Works
When you use theStac widget to fetch screens from Stac Cloud:
- First Load: The screen is fetched from the network and stored in local cache
- Subsequent Loads: Based on your cache strategy, Stac returns cached data and/or fetches fresh data
- Version Tracking: Each cached screen stores its version number, enabling smart updates
- Background Updates: Fresh data can be fetched in the background without blocking the UI
Cache Strategies
Stac provides five caching strategies to fit different use cases:1. Optimistic (Default)
Returns cached data immediately while fetching updates in the background. Best for fast perceived performance.- ✅ Returns cached data instantly (even if expired)
- ✅ Fetches fresh data in background
- ✅ Updates cache for next load
- ⚡ Fastest perceived loading
2. Cache First
Uses cached data if available and valid, only fetches from network when cache is invalid or missing.- ✅ Uses cache if valid (not expired)
- ✅ Fetches from network only when cache is invalid
- ✅ Optionally refreshes in background
- 📱 Great for offline-first apps
3. Network First
Always tries the network first, falls back to cache if network fails.- ✅ Always fetches fresh data first
- ✅ Falls back to cache on network error
- ✅ Ensures latest content when online
- 🌐 Requires network for best experience
4. Cache Only
Only uses cached data, never makes network requests. Throws an error if no cache exists.- ✅ Never makes network requests
- ✅ Instant loading from cache
- ❌ Fails if no cached data exists
- 📴 Perfect for offline mode
5. Network Only
Always fetches from network, never uses or updates cache.- ✅ Always fresh data
- ❌ Fails without network
- ❌ No offline support
- 🔒 Good for sensitive data
Cache Configuration
StacCacheConfig Properties
| Property | Type | Default | Description |
|---|---|---|---|
strategy | StacCacheStrategy | optimistic | The caching strategy to use |
maxAge | Duration? | null | Maximum age before cache is considered expired. null means no time-based expiration |
refreshInBackground | bool | true | Whether to fetch fresh data in background when cache is valid |
staleWhileRevalidate | bool | false | Whether to use expired cache while fetching fresh data |
Setting Cache Duration
Control how long cached data remains valid:Background Refresh
Keep cache fresh without blocking the UI:refreshInBackground is true:
- Valid cache is returned immediately
- Fresh data is fetched in background
- Cache is updated for next load
- User sees instant loading with eventual consistency
Stale While Revalidate
Show expired cache while fetching fresh data:- You prefer showing something over a loading spinner
- Content staleness is acceptable for a brief period
- Network is slow or unreliable
Strategy Comparison
| Strategy | Initial Load | Subsequent Load | Offline Support | Best For |
|---|---|---|---|---|
optimistic | Network → Cache | Cache (bg update) | ✅ Yes | Fast UX |
cacheFirst | Cache or Network | Cache | ✅ Yes | Offline apps |
networkFirst | Network | Network (cache fallback) | ⚠️ Fallback only | Fresh data |
cacheOnly | Cache only | Cache only | ✅ Yes | Offline mode |
networkOnly | Network only | Network only | ❌ No | Sensitive data |
Version-Based Updates
Stac tracks version numbers for each cached screen. When you deploy updates to Stac Cloud:- The server assigns a new version number
- Background fetches detect the new version
- Cache is updated with new content
- Next load shows updated screen