|
| 1 | +--- |
| 2 | +title: "Caching & Offline Support" |
| 3 | +description: "Learn how Stac intelligently caches screens for offline access, faster loading, and smarter network usage" |
| 4 | +--- |
| 5 | + |
| 6 | +Stac includes a powerful caching system that stores screens locally, enabling offline access, instant loading, and more efficient network usage. The caching layer works automatically with the `Stac` widget and can be customized for different use cases. |
| 7 | + |
| 8 | +## How Caching Works |
| 9 | + |
| 10 | +When you use the `Stac` widget to fetch screens from Stac Cloud: |
| 11 | + |
| 12 | +1. **First Load**: The screen is fetched from the network and stored in local cache |
| 13 | +2. **Subsequent Loads**: Based on your cache strategy, Stac returns cached data and/or fetches fresh data |
| 14 | +3. **Version Tracking**: Each cached screen stores its version number, enabling smart updates |
| 15 | +4. **Background Updates**: Fresh data can be fetched in the background without blocking the UI |
| 16 | + |
| 17 | +## Cache Strategies |
| 18 | + |
| 19 | +Stac provides five caching strategies to fit different use cases: |
| 20 | + |
| 21 | +### 1. Optimistic (Default) |
| 22 | + |
| 23 | +Returns cached data immediately while fetching updates in the background. Best for fast perceived performance. |
| 24 | + |
| 25 | +```dart |
| 26 | +Stac( |
| 27 | + routeName: '/home', |
| 28 | + cacheConfig: StacCacheConfig( |
| 29 | + strategy: StacCacheStrategy.optimistic, |
| 30 | + ), |
| 31 | +) |
| 32 | +``` |
| 33 | + |
| 34 | +**Behavior:** |
| 35 | +- ✅ Returns cached data instantly (even if expired) |
| 36 | +- ✅ Fetches fresh data in background |
| 37 | +- ✅ Updates cache for next load |
| 38 | +- ⚡ Fastest perceived loading |
| 39 | + |
| 40 | +**Best for:** UI layouts, content screens, any screen where instant loading matters more than showing the absolute latest data. |
| 41 | + |
| 42 | +### 2. Cache First |
| 43 | + |
| 44 | +Uses cached data if available and valid, only fetches from network when cache is invalid or missing. |
| 45 | + |
| 46 | +```dart |
| 47 | +Stac( |
| 48 | + routeName: '/home', |
| 49 | + cacheConfig: StacCacheConfig( |
| 50 | + strategy: StacCacheStrategy.cacheFirst, |
| 51 | + maxAge: Duration(hours: 24), |
| 52 | + ), |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +**Behavior:** |
| 57 | +- ✅ Uses cache if valid (not expired) |
| 58 | +- ✅ Fetches from network only when cache is invalid |
| 59 | +- ✅ Optionally refreshes in background |
| 60 | +- 📱 Great for offline-first apps |
| 61 | + |
| 62 | +**Best for:** Offline-first apps, content that doesn't change frequently, reducing network usage. |
| 63 | + |
| 64 | +### 3. Network First |
| 65 | + |
| 66 | +Always tries the network first, falls back to cache if network fails. |
| 67 | + |
| 68 | +```dart |
| 69 | +Stac( |
| 70 | + routeName: '/home', |
| 71 | + cacheConfig: StacCacheConfig( |
| 72 | + strategy: StacCacheStrategy.networkFirst, |
| 73 | + ), |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +**Behavior:** |
| 78 | +- ✅ Always fetches fresh data first |
| 79 | +- ✅ Falls back to cache on network error |
| 80 | +- ✅ Ensures latest content when online |
| 81 | +- 🌐 Requires network for best experience |
| 82 | + |
| 83 | +**Best for:** Real-time data, frequently changing content, screens where freshness is critical. |
| 84 | + |
| 85 | +### 4. Cache Only |
| 86 | + |
| 87 | +Only uses cached data, never makes network requests. Throws an error if no cache exists. |
| 88 | + |
| 89 | +```dart |
| 90 | +Stac( |
| 91 | + routeName: '/home', |
| 92 | + cacheConfig: StacCacheConfig( |
| 93 | + strategy: StacCacheStrategy.cacheOnly, |
| 94 | + ), |
| 95 | +) |
| 96 | +``` |
| 97 | + |
| 98 | +**Behavior:** |
| 99 | +- ✅ Never makes network requests |
| 100 | +- ✅ Instant loading from cache |
| 101 | +- ❌ Fails if no cached data exists |
| 102 | +- 📴 Perfect for offline mode |
| 103 | + |
| 104 | +**Best for:** Offline-only mode, airplane mode, when you've pre-cached screens. |
| 105 | + |
| 106 | +### 5. Network Only |
| 107 | + |
| 108 | +Always fetches from network, never uses or updates cache. |
| 109 | + |
| 110 | +```dart |
| 111 | +Stac( |
| 112 | + routeName: '/home', |
| 113 | + cacheConfig: StacCacheConfig( |
| 114 | + strategy: StacCacheStrategy.networkOnly, |
| 115 | + ), |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +**Behavior:** |
| 120 | +- ✅ Always fresh data |
| 121 | +- ❌ Fails without network |
| 122 | +- ❌ No offline support |
| 123 | +- 🔒 Good for sensitive data |
| 124 | + |
| 125 | +**Best for:** Sensitive data that shouldn't be cached, real-time dashboards, authentication screens. |
| 126 | + |
| 127 | +## Cache Configuration |
| 128 | + |
| 129 | +### StacCacheConfig Properties |
| 130 | + |
| 131 | +| Property | Type | Default | Description | |
| 132 | +|----------|------|---------|-------------| |
| 133 | +| `strategy` | `StacCacheStrategy` | `optimistic` | The caching strategy to use | |
| 134 | +| `maxAge` | `Duration?` | `null` | Maximum age before cache is considered expired. `null` means no time-based expiration | |
| 135 | +| `refreshInBackground` | `bool` | `true` | Whether to fetch fresh data in background when cache is valid | |
| 136 | +| `staleWhileRevalidate` | `bool` | `false` | Whether to use expired cache while fetching fresh data | |
| 137 | + |
| 138 | +### Setting Cache Duration |
| 139 | + |
| 140 | +Control how long cached data remains valid: |
| 141 | + |
| 142 | +```dart |
| 143 | +// Cache valid for 1 hour |
| 144 | +Stac( |
| 145 | + routeName: '/home', |
| 146 | + cacheConfig: StacCacheConfig( |
| 147 | + strategy: StacCacheStrategy.cacheFirst, |
| 148 | + maxAge: Duration(hours: 1), |
| 149 | + ), |
| 150 | +) |
| 151 | +
|
| 152 | +// Cache valid for 7 days |
| 153 | +Stac( |
| 154 | + routeName: '/settings', |
| 155 | + cacheConfig: StacCacheConfig( |
| 156 | + strategy: StacCacheStrategy.cacheFirst, |
| 157 | + maxAge: Duration(days: 7), |
| 158 | + ), |
| 159 | +) |
| 160 | +
|
| 161 | +// Cache never expires (only version-based updates) |
| 162 | +Stac( |
| 163 | + routeName: '/static-page', |
| 164 | + cacheConfig: StacCacheConfig( |
| 165 | + strategy: StacCacheStrategy.cacheFirst, |
| 166 | + maxAge: null, // No time-based expiration |
| 167 | + ), |
| 168 | +) |
| 169 | +``` |
| 170 | + |
| 171 | +### Background Refresh |
| 172 | + |
| 173 | +Keep cache fresh without blocking the UI: |
| 174 | + |
| 175 | +```dart |
| 176 | +Stac( |
| 177 | + routeName: '/home', |
| 178 | + cacheConfig: StacCacheConfig( |
| 179 | + strategy: StacCacheStrategy.cacheFirst, |
| 180 | + maxAge: Duration(hours: 1), |
| 181 | + refreshInBackground: true, // Fetch updates silently |
| 182 | + ), |
| 183 | +) |
| 184 | +``` |
| 185 | + |
| 186 | +When `refreshInBackground` is `true`: |
| 187 | +- Valid cache is returned immediately |
| 188 | +- Fresh data is fetched in background |
| 189 | +- Cache is updated for next load |
| 190 | +- User sees instant loading with eventual consistency |
| 191 | + |
| 192 | +### Stale While Revalidate |
| 193 | + |
| 194 | +Show expired cache while fetching fresh data: |
| 195 | + |
| 196 | +```dart |
| 197 | +Stac( |
| 198 | + routeName: '/home', |
| 199 | + cacheConfig: StacCacheConfig( |
| 200 | + strategy: StacCacheStrategy.cacheFirst, |
| 201 | + maxAge: Duration(hours: 1), |
| 202 | + staleWhileRevalidate: true, // Use expired cache while fetching |
| 203 | + ), |
| 204 | +) |
| 205 | +``` |
| 206 | + |
| 207 | +This is useful when: |
| 208 | +- You prefer showing something over a loading spinner |
| 209 | +- Content staleness is acceptable for a brief period |
| 210 | +- Network is slow or unreliable |
| 211 | + |
| 212 | +## Strategy Comparison |
| 213 | + |
| 214 | +| Strategy | Initial Load | Subsequent Load | Offline Support | Best For | |
| 215 | +|----------|--------------|-----------------|-----------------|----------| |
| 216 | +| `optimistic` | Network → Cache | Cache (bg update) | ✅ Yes | Fast UX | |
| 217 | +| `cacheFirst` | Cache or Network | Cache | ✅ Yes | Offline apps | |
| 218 | +| `networkFirst` | Network | Network (cache fallback) | ⚠️ Fallback only | Fresh data | |
| 219 | +| `cacheOnly` | Cache only | Cache only | ✅ Yes | Offline mode | |
| 220 | +| `networkOnly` | Network only | Network only | ❌ No | Sensitive data | |
| 221 | + |
| 222 | +## Version-Based Updates |
| 223 | + |
| 224 | +Stac tracks version numbers for each cached screen. When you deploy updates to Stac Cloud: |
| 225 | + |
| 226 | +1. The server assigns a new version number |
| 227 | +2. Background fetches detect the new version |
| 228 | +3. Cache is updated with new content |
| 229 | +4. Next load shows updated screen |
| 230 | + |
| 231 | +This ensures users get updates without manual intervention while maintaining fast loading times. |
| 232 | + |
0 commit comments