@proveanything/smartlinks 1.4.7 → 1.5.0
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/api/appObjects.d.ts +127 -0
- package/dist/api/appObjects.js +257 -0
- package/dist/api/appRecord.d.ts +0 -6
- package/dist/api/appRecord.js +1 -29
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/cache.js +21 -0
- package/dist/docs/API_SUMMARY.md +468 -13
- package/dist/docs/app-objects.md +954 -0
- package/dist/docs/caching.md +186 -0
- package/dist/http.d.ts +6 -0
- package/dist/http.js +34 -0
- package/dist/types/appObjects.d.ts +371 -0
- package/dist/types/appObjects.js +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/docs/API_SUMMARY.md +468 -13
- package/docs/app-objects.md +954 -0
- package/docs/caching.md +186 -0
- package/package.json +1 -1
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
|
+
```
|