@proveanything/smartlinks 1.4.7 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache.js +21 -0
- package/dist/docs/API_SUMMARY.md +3 -2
- package/dist/docs/caching.md +186 -0
- package/dist/http.d.ts +6 -0
- package/dist/http.js +34 -0
- package/docs/API_SUMMARY.md +3 -2
- package/docs/caching.md +186 -0
- package/package.json +1 -1
package/dist/cache.js
CHANGED
|
@@ -3,6 +3,27 @@
|
|
|
3
3
|
// =============================================================================
|
|
4
4
|
// In-memory cache store
|
|
5
5
|
const memoryCache = new Map();
|
|
6
|
+
/**
|
|
7
|
+
* Clear session-scoped caches on page load.
|
|
8
|
+
* SessionStorage persists across normal refreshes, so we explicitly clear it
|
|
9
|
+
* on load to ensure fresh data after F5/Ctrl+F5. LocalStorage is preserved
|
|
10
|
+
* for true offline/persistent caching scenarios.
|
|
11
|
+
*/
|
|
12
|
+
function clearSessionCacheOnLoad() {
|
|
13
|
+
if (typeof sessionStorage === 'undefined')
|
|
14
|
+
return;
|
|
15
|
+
try {
|
|
16
|
+
const sessionKeys = Object.keys(sessionStorage).filter(k => k.startsWith('smartlinks:cache:'));
|
|
17
|
+
sessionKeys.forEach(k => sessionStorage.removeItem(k));
|
|
18
|
+
}
|
|
19
|
+
catch (_a) {
|
|
20
|
+
// Storage may not be available
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Auto-clear session caches on page load (browser only)
|
|
24
|
+
if (typeof window !== 'undefined') {
|
|
25
|
+
clearSessionCacheOnLoad();
|
|
26
|
+
}
|
|
6
27
|
/**
|
|
7
28
|
* Get cached value or fetch fresh.
|
|
8
29
|
*
|
package/dist/docs/API_SUMMARY.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Smartlinks API Summary
|
|
2
2
|
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.8 | Generated: 2026-02-22T11:38:27.387Z
|
|
4
4
|
|
|
5
5
|
This is a concise summary of all available API functions and types.
|
|
6
6
|
|
|
@@ -120,8 +120,9 @@ Returns true if the SDK currently has any auth credential set (bearer token or A
|
|
|
120
120
|
persistence?: 'none' | 'indexeddb'
|
|
121
121
|
persistenceTtlMs?: number
|
|
122
122
|
serveStaleOnOffline?: boolean
|
|
123
|
+
clearOnPageLoad?: boolean
|
|
123
124
|
}) → `void`
|
|
124
|
-
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls. All GET requests benefit automatically. Per-resource rules (collections/products → 1 h, proofs → 30 s, etc.) override this value. in-memory only (`'none'`, default). Ignored in Node.js. fallback, from the original fetch time (default: 7 days). `SmartlinksOfflineError` with stale data instead of propagating the network error. ```ts // Enable IndexedDB persistence for offline support configureSdkCache({ persistence: 'indexeddb' }) // Disable cache entirely in test environments configureSdkCache({ enabled: false }) ```
|
|
125
|
+
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls. All GET requests benefit automatically. Per-resource rules (collections/products → 1 h, proofs → 30 s, etc.) override this value. in-memory only (`'none'`, default). Ignored in Node.js. fallback, from the original fetch time (default: 7 days). `SmartlinksOfflineError` with stale data instead of propagating the network error. caches on page load/refresh. IndexedDB persists for offline. ```ts // Enable IndexedDB persistence for offline support configureSdkCache({ persistence: 'indexeddb' }) // Disable cache entirely in test environments configureSdkCache({ enabled: false }) // Keep caches across page refreshes (not recommended for production) configureSdkCache({ clearOnPageLoad: false }) ```
|
|
125
126
|
|
|
126
127
|
**invalidateCache**(urlPattern?: string) → `void`
|
|
127
128
|
Manually invalidate entries in the SDK's GET cache. *contains* this string is removed. Omit (or pass `undefined`) to wipe the entire cache. ```ts invalidateCache() // clear everything invalidateCache('/collection/abc123') // one specific collection invalidateCache('/product/') // all product responses ```
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# Caching Strategy
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The Smartlinks SDK implements a multi-tier caching system designed to balance performance with data freshness:
|
|
6
|
+
|
|
7
|
+
1. **In-Memory Cache (L1)** - Fastest, cleared on page refresh
|
|
8
|
+
2. **SessionStorage** - Cleared on page refresh (not tab close)
|
|
9
|
+
3. **IndexedDB (L2)** - Persists across refreshes for offline support
|
|
10
|
+
|
|
11
|
+
## Cache Behavior on Page Refresh
|
|
12
|
+
|
|
13
|
+
### Default Behavior (Recommended)
|
|
14
|
+
|
|
15
|
+
When you refresh the page (F5, Ctrl+F5, or window reload):
|
|
16
|
+
|
|
17
|
+
- ✅ **In-memory cache** - Automatically cleared (module re-initialization)
|
|
18
|
+
- ✅ **SessionStorage** - Explicitly cleared on page load
|
|
19
|
+
- ✅ **IndexedDB** - Preserved for offline fallback only
|
|
20
|
+
|
|
21
|
+
This ensures that **page refreshes always fetch fresh data from the server**, while maintaining offline capabilities.
|
|
22
|
+
|
|
23
|
+
### How It Works
|
|
24
|
+
|
|
25
|
+
1. **During browsing (same page load)**:
|
|
26
|
+
- API requests are cached in memory and sessionStorage
|
|
27
|
+
- Subsequent identical requests serve from cache (fast!)
|
|
28
|
+
- TTL rules determine cache freshness (30s - 1h depending on resource)
|
|
29
|
+
|
|
30
|
+
2. **On page refresh**:
|
|
31
|
+
- Module re-initialization clears in-memory cache
|
|
32
|
+
- `clearSessionCacheOnLoad()` runs automatically
|
|
33
|
+
- SessionStorage caches are removed
|
|
34
|
+
- Fresh network requests are made
|
|
35
|
+
|
|
36
|
+
3. **When offline** (with `persistence: 'indexeddb'`):
|
|
37
|
+
- Network request fails
|
|
38
|
+
- SDK checks IndexedDB for stale data
|
|
39
|
+
- If found and within 7-day TTL, throws `SmartlinksOfflineError` with data
|
|
40
|
+
- App can catch this and use stale data gracefully
|
|
41
|
+
|
|
42
|
+
## Cache Configuration
|
|
43
|
+
|
|
44
|
+
### Enable IndexedDB Persistence
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { configureSdkCache } from '@smartlinks/sdk';
|
|
48
|
+
|
|
49
|
+
// Enable offline support via IndexedDB
|
|
50
|
+
configureSdkCache({
|
|
51
|
+
persistence: 'indexeddb',
|
|
52
|
+
persistenceTtlMs: 7 * 24 * 60 * 60_000, // 7 days
|
|
53
|
+
serveStaleOnOffline: true,
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Disable Clear-on-Refresh (Not Recommended)
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Keep caches across page refreshes
|
|
61
|
+
// ⚠️ Not recommended for production - you'll serve stale data after refresh
|
|
62
|
+
configureSdkCache({
|
|
63
|
+
clearOnPageLoad: false,
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Disable Caching Entirely
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Useful for testing or debugging
|
|
71
|
+
configureSdkCache({
|
|
72
|
+
enabled: false,
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Cache Storage Types
|
|
77
|
+
|
|
78
|
+
### `storage: 'memory'` (Default)
|
|
79
|
+
- Lives in JavaScript memory (Map)
|
|
80
|
+
- Cleared on page refresh
|
|
81
|
+
- Fastest access
|
|
82
|
+
- No quota limits
|
|
83
|
+
|
|
84
|
+
### `storage: 'session'`
|
|
85
|
+
- Lives in `sessionStorage`
|
|
86
|
+
- **Cleared on page load** (new behavior)
|
|
87
|
+
- Survives navigation within same session
|
|
88
|
+
- ~5-10MB quota
|
|
89
|
+
|
|
90
|
+
### `storage: 'local'`
|
|
91
|
+
- Lives in `localStorage`
|
|
92
|
+
- Persists across browser restarts
|
|
93
|
+
- Use sparingly for truly persistent data
|
|
94
|
+
- ~5-10MB quota
|
|
95
|
+
|
|
96
|
+
### `persistence: 'indexeddb'`
|
|
97
|
+
- Lives in IndexedDB (L2 cache)
|
|
98
|
+
- Persists across refreshes and restarts
|
|
99
|
+
- **Only used as offline fallback**, not for normal requests
|
|
100
|
+
- ~50MB+ quota
|
|
101
|
+
|
|
102
|
+
## TTL Rules
|
|
103
|
+
|
|
104
|
+
Different API resources have different cache lifetimes:
|
|
105
|
+
|
|
106
|
+
| Resource | TTL | Reason |
|
|
107
|
+
|----------|-----|--------|
|
|
108
|
+
| `/proof/*` | 30 seconds | Proofs change frequently |
|
|
109
|
+
| `/attestation/*` | 2 minutes | Attestations update regularly |
|
|
110
|
+
| `/product/*` | 1 hour | Products are relatively stable |
|
|
111
|
+
| `/variant/*` | 1 hour | Variants rarely change |
|
|
112
|
+
| `/collection/*` | 1 hour | Collections are stable |
|
|
113
|
+
| Everything else | 1 minute | Default safety |
|
|
114
|
+
|
|
115
|
+
## Manual Cache Control
|
|
116
|
+
|
|
117
|
+
### Invalidate Specific Resource
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { invalidateCache } from '@smartlinks/sdk';
|
|
121
|
+
|
|
122
|
+
// Clear cache for a specific collection
|
|
123
|
+
invalidateCache('/collection/abc123');
|
|
124
|
+
|
|
125
|
+
// Clear all product caches
|
|
126
|
+
invalidateCache('/product/');
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Clear All Caches
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { invalidateCache } from '@smartlinks/sdk';
|
|
133
|
+
|
|
134
|
+
// Nuclear option - clear everything
|
|
135
|
+
invalidateCache();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Best Practices
|
|
139
|
+
|
|
140
|
+
1. **Default is best** - The SDK defaults are designed for optimal behavior
|
|
141
|
+
2. **Enable IndexedDB for PWAs** - If building offline-first apps
|
|
142
|
+
3. **Don't disable clearOnPageLoad** - Users expect fresh data after refresh
|
|
143
|
+
4. **Use invalidateCache() after mutations** - SDK does this automatically for you
|
|
144
|
+
5. **Trust the TTL rules** - They're tuned for smartlinks.app API behavior
|
|
145
|
+
|
|
146
|
+
## Offline Mode Example
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { collection, SmartlinksOfflineError } from '@smartlinks/sdk';
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const data = await collection.get('abc123');
|
|
153
|
+
// Fresh data from server
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (error instanceof SmartlinksOfflineError) {
|
|
156
|
+
// Network failed, but we have stale data
|
|
157
|
+
console.warn('Offline - using stale data from', error.cachedAt);
|
|
158
|
+
const staleData = error.data;
|
|
159
|
+
// Display stale data with a banner
|
|
160
|
+
} else {
|
|
161
|
+
// Real error - no offline data available
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Migration from Old Behavior
|
|
168
|
+
|
|
169
|
+
If you previously relied on caches persisting across refreshes:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Old (implicit) behavior:
|
|
173
|
+
// - SessionStorage survived refreshes
|
|
174
|
+
// - Apps might see stale data after F5
|
|
175
|
+
|
|
176
|
+
// New (explicit) behavior:
|
|
177
|
+
configureSdkCache({
|
|
178
|
+
clearOnPageLoad: true, // default - fresh data on refresh
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// If you REALLY need old behavior (not recommended):
|
|
182
|
+
configureSdkCache({
|
|
183
|
+
clearOnPageLoad: false,
|
|
184
|
+
persistence: 'indexeddb', // move to IndexedDB instead
|
|
185
|
+
});
|
|
186
|
+
```
|
package/dist/http.d.ts
CHANGED
|
@@ -82,6 +82,8 @@ export declare function hasAuthCredentials(): boolean;
|
|
|
82
82
|
* @param options.serveStaleOnOffline - When `true` (default) and persistence is on, throw
|
|
83
83
|
* `SmartlinksOfflineError` with stale data instead of
|
|
84
84
|
* propagating the network error.
|
|
85
|
+
* @param options.clearOnPageLoad - When `true` (default), clear in-memory and sessionStorage
|
|
86
|
+
* caches on page load/refresh. IndexedDB persists for offline.
|
|
85
87
|
*
|
|
86
88
|
* @example
|
|
87
89
|
* ```ts
|
|
@@ -90,6 +92,9 @@ export declare function hasAuthCredentials(): boolean;
|
|
|
90
92
|
*
|
|
91
93
|
* // Disable cache entirely in test environments
|
|
92
94
|
* configureSdkCache({ enabled: false })
|
|
95
|
+
*
|
|
96
|
+
* // Keep caches across page refreshes (not recommended for production)
|
|
97
|
+
* configureSdkCache({ clearOnPageLoad: false })
|
|
93
98
|
* ```
|
|
94
99
|
*/
|
|
95
100
|
export declare function configureSdkCache(options: {
|
|
@@ -99,6 +104,7 @@ export declare function configureSdkCache(options: {
|
|
|
99
104
|
persistence?: 'none' | 'indexeddb';
|
|
100
105
|
persistenceTtlMs?: number;
|
|
101
106
|
serveStaleOnOffline?: boolean;
|
|
107
|
+
clearOnPageLoad?: boolean;
|
|
102
108
|
}): void;
|
|
103
109
|
/**
|
|
104
110
|
* Manually invalidate entries in the SDK's GET cache.
|
package/dist/http.js
CHANGED
|
@@ -31,6 +31,8 @@ let cacheDefaultTtlMs = 60000; // 60 seconds
|
|
|
31
31
|
let cacheMaxEntries = 200;
|
|
32
32
|
/** Persistence backend for the L2 cache. 'none' (default) disables IndexedDB persistence. */
|
|
33
33
|
let cachePersistence = 'none';
|
|
34
|
+
/** When true (default), clear in-memory and sessionStorage caches on page load/refresh. */
|
|
35
|
+
let cacheClearOnPageLoad = true;
|
|
34
36
|
/**
|
|
35
37
|
* How long L2 (IndexedDB) entries are considered valid as an offline stale fallback,
|
|
36
38
|
* measured from the original network fetch time (default: 7 days).
|
|
@@ -89,6 +91,31 @@ function evictLruIfNeeded() {
|
|
|
89
91
|
break;
|
|
90
92
|
}
|
|
91
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* Clear session-scoped caches (in-memory + sessionStorage) on page load.
|
|
96
|
+
* This ensures that a page refresh always fetches fresh data, while
|
|
97
|
+
* IndexedDB persists for true offline support.
|
|
98
|
+
*
|
|
99
|
+
* Automatically called once when this module loads in a browser environment.
|
|
100
|
+
*/
|
|
101
|
+
function clearSessionCachesOnPageLoad() {
|
|
102
|
+
if (typeof window === 'undefined')
|
|
103
|
+
return; // Node.js environment
|
|
104
|
+
httpCache.clear();
|
|
105
|
+
try {
|
|
106
|
+
if (typeof sessionStorage !== 'undefined') {
|
|
107
|
+
const sessionKeys = Object.keys(sessionStorage).filter(k => k.startsWith('smartlinks:cache:'));
|
|
108
|
+
sessionKeys.forEach(k => sessionStorage.removeItem(k));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (_a) {
|
|
112
|
+
// Storage may not be available (private browsing, etc.)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Auto-clear session caches on page load (browser only)
|
|
116
|
+
if (typeof window !== 'undefined' && cacheClearOnPageLoad) {
|
|
117
|
+
clearSessionCachesOnPageLoad();
|
|
118
|
+
}
|
|
92
119
|
/**
|
|
93
120
|
* Return cached data for a key if it exists and is within TTL.
|
|
94
121
|
* Promotes the hit to MRU position. Returns null when missing, expired, or in-flight.
|
|
@@ -416,6 +443,8 @@ export function hasAuthCredentials() {
|
|
|
416
443
|
* @param options.serveStaleOnOffline - When `true` (default) and persistence is on, throw
|
|
417
444
|
* `SmartlinksOfflineError` with stale data instead of
|
|
418
445
|
* propagating the network error.
|
|
446
|
+
* @param options.clearOnPageLoad - When `true` (default), clear in-memory and sessionStorage
|
|
447
|
+
* caches on page load/refresh. IndexedDB persists for offline.
|
|
419
448
|
*
|
|
420
449
|
* @example
|
|
421
450
|
* ```ts
|
|
@@ -424,6 +453,9 @@ export function hasAuthCredentials() {
|
|
|
424
453
|
*
|
|
425
454
|
* // Disable cache entirely in test environments
|
|
426
455
|
* configureSdkCache({ enabled: false })
|
|
456
|
+
*
|
|
457
|
+
* // Keep caches across page refreshes (not recommended for production)
|
|
458
|
+
* configureSdkCache({ clearOnPageLoad: false })
|
|
427
459
|
* ```
|
|
428
460
|
*/
|
|
429
461
|
export function configureSdkCache(options) {
|
|
@@ -439,6 +471,8 @@ export function configureSdkCache(options) {
|
|
|
439
471
|
cachePersistenceTtlMs = options.persistenceTtlMs;
|
|
440
472
|
if (options.serveStaleOnOffline !== undefined)
|
|
441
473
|
cacheServeStaleOnOffline = options.serveStaleOnOffline;
|
|
474
|
+
if (options.clearOnPageLoad !== undefined)
|
|
475
|
+
cacheClearOnPageLoad = options.clearOnPageLoad;
|
|
442
476
|
}
|
|
443
477
|
/**
|
|
444
478
|
* Manually invalidate entries in the SDK's GET cache.
|
package/docs/API_SUMMARY.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Smartlinks API Summary
|
|
2
2
|
|
|
3
|
-
Version: 1.4.
|
|
3
|
+
Version: 1.4.8 | Generated: 2026-02-22T11:38:27.387Z
|
|
4
4
|
|
|
5
5
|
This is a concise summary of all available API functions and types.
|
|
6
6
|
|
|
@@ -120,8 +120,9 @@ Returns true if the SDK currently has any auth credential set (bearer token or A
|
|
|
120
120
|
persistence?: 'none' | 'indexeddb'
|
|
121
121
|
persistenceTtlMs?: number
|
|
122
122
|
serveStaleOnOffline?: boolean
|
|
123
|
+
clearOnPageLoad?: boolean
|
|
123
124
|
}) → `void`
|
|
124
|
-
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls. All GET requests benefit automatically. Per-resource rules (collections/products → 1 h, proofs → 30 s, etc.) override this value. in-memory only (`'none'`, default). Ignored in Node.js. fallback, from the original fetch time (default: 7 days). `SmartlinksOfflineError` with stale data instead of propagating the network error. ```ts // Enable IndexedDB persistence for offline support configureSdkCache({ persistence: 'indexeddb' }) // Disable cache entirely in test environments configureSdkCache({ enabled: false }) ```
|
|
125
|
+
Configure the SDK's built-in in-memory GET cache. The cache is transparent — it sits inside the HTTP layer and requires no changes to your existing API calls. All GET requests benefit automatically. Per-resource rules (collections/products → 1 h, proofs → 30 s, etc.) override this value. in-memory only (`'none'`, default). Ignored in Node.js. fallback, from the original fetch time (default: 7 days). `SmartlinksOfflineError` with stale data instead of propagating the network error. caches on page load/refresh. IndexedDB persists for offline. ```ts // Enable IndexedDB persistence for offline support configureSdkCache({ persistence: 'indexeddb' }) // Disable cache entirely in test environments configureSdkCache({ enabled: false }) // Keep caches across page refreshes (not recommended for production) configureSdkCache({ clearOnPageLoad: false }) ```
|
|
125
126
|
|
|
126
127
|
**invalidateCache**(urlPattern?: string) → `void`
|
|
127
128
|
Manually invalidate entries in the SDK's GET cache. *contains* this string is removed. Omit (or pass `undefined`) to wipe the entire cache. ```ts invalidateCache() // clear everything invalidateCache('/collection/abc123') // one specific collection invalidateCache('/product/') // all product responses ```
|
package/docs/caching.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# Caching Strategy
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The Smartlinks SDK implements a multi-tier caching system designed to balance performance with data freshness:
|
|
6
|
+
|
|
7
|
+
1. **In-Memory Cache (L1)** - Fastest, cleared on page refresh
|
|
8
|
+
2. **SessionStorage** - Cleared on page refresh (not tab close)
|
|
9
|
+
3. **IndexedDB (L2)** - Persists across refreshes for offline support
|
|
10
|
+
|
|
11
|
+
## Cache Behavior on Page Refresh
|
|
12
|
+
|
|
13
|
+
### Default Behavior (Recommended)
|
|
14
|
+
|
|
15
|
+
When you refresh the page (F5, Ctrl+F5, or window reload):
|
|
16
|
+
|
|
17
|
+
- ✅ **In-memory cache** - Automatically cleared (module re-initialization)
|
|
18
|
+
- ✅ **SessionStorage** - Explicitly cleared on page load
|
|
19
|
+
- ✅ **IndexedDB** - Preserved for offline fallback only
|
|
20
|
+
|
|
21
|
+
This ensures that **page refreshes always fetch fresh data from the server**, while maintaining offline capabilities.
|
|
22
|
+
|
|
23
|
+
### How It Works
|
|
24
|
+
|
|
25
|
+
1. **During browsing (same page load)**:
|
|
26
|
+
- API requests are cached in memory and sessionStorage
|
|
27
|
+
- Subsequent identical requests serve from cache (fast!)
|
|
28
|
+
- TTL rules determine cache freshness (30s - 1h depending on resource)
|
|
29
|
+
|
|
30
|
+
2. **On page refresh**:
|
|
31
|
+
- Module re-initialization clears in-memory cache
|
|
32
|
+
- `clearSessionCacheOnLoad()` runs automatically
|
|
33
|
+
- SessionStorage caches are removed
|
|
34
|
+
- Fresh network requests are made
|
|
35
|
+
|
|
36
|
+
3. **When offline** (with `persistence: 'indexeddb'`):
|
|
37
|
+
- Network request fails
|
|
38
|
+
- SDK checks IndexedDB for stale data
|
|
39
|
+
- If found and within 7-day TTL, throws `SmartlinksOfflineError` with data
|
|
40
|
+
- App can catch this and use stale data gracefully
|
|
41
|
+
|
|
42
|
+
## Cache Configuration
|
|
43
|
+
|
|
44
|
+
### Enable IndexedDB Persistence
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { configureSdkCache } from '@smartlinks/sdk';
|
|
48
|
+
|
|
49
|
+
// Enable offline support via IndexedDB
|
|
50
|
+
configureSdkCache({
|
|
51
|
+
persistence: 'indexeddb',
|
|
52
|
+
persistenceTtlMs: 7 * 24 * 60 * 60_000, // 7 days
|
|
53
|
+
serveStaleOnOffline: true,
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Disable Clear-on-Refresh (Not Recommended)
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Keep caches across page refreshes
|
|
61
|
+
// ⚠️ Not recommended for production - you'll serve stale data after refresh
|
|
62
|
+
configureSdkCache({
|
|
63
|
+
clearOnPageLoad: false,
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Disable Caching Entirely
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
// Useful for testing or debugging
|
|
71
|
+
configureSdkCache({
|
|
72
|
+
enabled: false,
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Cache Storage Types
|
|
77
|
+
|
|
78
|
+
### `storage: 'memory'` (Default)
|
|
79
|
+
- Lives in JavaScript memory (Map)
|
|
80
|
+
- Cleared on page refresh
|
|
81
|
+
- Fastest access
|
|
82
|
+
- No quota limits
|
|
83
|
+
|
|
84
|
+
### `storage: 'session'`
|
|
85
|
+
- Lives in `sessionStorage`
|
|
86
|
+
- **Cleared on page load** (new behavior)
|
|
87
|
+
- Survives navigation within same session
|
|
88
|
+
- ~5-10MB quota
|
|
89
|
+
|
|
90
|
+
### `storage: 'local'`
|
|
91
|
+
- Lives in `localStorage`
|
|
92
|
+
- Persists across browser restarts
|
|
93
|
+
- Use sparingly for truly persistent data
|
|
94
|
+
- ~5-10MB quota
|
|
95
|
+
|
|
96
|
+
### `persistence: 'indexeddb'`
|
|
97
|
+
- Lives in IndexedDB (L2 cache)
|
|
98
|
+
- Persists across refreshes and restarts
|
|
99
|
+
- **Only used as offline fallback**, not for normal requests
|
|
100
|
+
- ~50MB+ quota
|
|
101
|
+
|
|
102
|
+
## TTL Rules
|
|
103
|
+
|
|
104
|
+
Different API resources have different cache lifetimes:
|
|
105
|
+
|
|
106
|
+
| Resource | TTL | Reason |
|
|
107
|
+
|----------|-----|--------|
|
|
108
|
+
| `/proof/*` | 30 seconds | Proofs change frequently |
|
|
109
|
+
| `/attestation/*` | 2 minutes | Attestations update regularly |
|
|
110
|
+
| `/product/*` | 1 hour | Products are relatively stable |
|
|
111
|
+
| `/variant/*` | 1 hour | Variants rarely change |
|
|
112
|
+
| `/collection/*` | 1 hour | Collections are stable |
|
|
113
|
+
| Everything else | 1 minute | Default safety |
|
|
114
|
+
|
|
115
|
+
## Manual Cache Control
|
|
116
|
+
|
|
117
|
+
### Invalidate Specific Resource
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { invalidateCache } from '@smartlinks/sdk';
|
|
121
|
+
|
|
122
|
+
// Clear cache for a specific collection
|
|
123
|
+
invalidateCache('/collection/abc123');
|
|
124
|
+
|
|
125
|
+
// Clear all product caches
|
|
126
|
+
invalidateCache('/product/');
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Clear All Caches
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { invalidateCache } from '@smartlinks/sdk';
|
|
133
|
+
|
|
134
|
+
// Nuclear option - clear everything
|
|
135
|
+
invalidateCache();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Best Practices
|
|
139
|
+
|
|
140
|
+
1. **Default is best** - The SDK defaults are designed for optimal behavior
|
|
141
|
+
2. **Enable IndexedDB for PWAs** - If building offline-first apps
|
|
142
|
+
3. **Don't disable clearOnPageLoad** - Users expect fresh data after refresh
|
|
143
|
+
4. **Use invalidateCache() after mutations** - SDK does this automatically for you
|
|
144
|
+
5. **Trust the TTL rules** - They're tuned for smartlinks.app API behavior
|
|
145
|
+
|
|
146
|
+
## Offline Mode Example
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { collection, SmartlinksOfflineError } from '@smartlinks/sdk';
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const data = await collection.get('abc123');
|
|
153
|
+
// Fresh data from server
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (error instanceof SmartlinksOfflineError) {
|
|
156
|
+
// Network failed, but we have stale data
|
|
157
|
+
console.warn('Offline - using stale data from', error.cachedAt);
|
|
158
|
+
const staleData = error.data;
|
|
159
|
+
// Display stale data with a banner
|
|
160
|
+
} else {
|
|
161
|
+
// Real error - no offline data available
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Migration from Old Behavior
|
|
168
|
+
|
|
169
|
+
If you previously relied on caches persisting across refreshes:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Old (implicit) behavior:
|
|
173
|
+
// - SessionStorage survived refreshes
|
|
174
|
+
// - Apps might see stale data after F5
|
|
175
|
+
|
|
176
|
+
// New (explicit) behavior:
|
|
177
|
+
configureSdkCache({
|
|
178
|
+
clearOnPageLoad: true, // default - fresh data on refresh
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// If you REALLY need old behavior (not recommended):
|
|
182
|
+
configureSdkCache({
|
|
183
|
+
clearOnPageLoad: false,
|
|
184
|
+
persistence: 'indexeddb', // move to IndexedDB instead
|
|
185
|
+
});
|
|
186
|
+
```
|