@brownandroot/api 1.2.0 → 1.2.1
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/README.md +1 -1
- package/dist/cache.js +8 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install @brownandroot/api
|
|
|
12
12
|
|
|
13
13
|
## Client-Side Cache API (recommended for SvelteKit)
|
|
14
14
|
|
|
15
|
-
The package ships a browser-side API at `@brownandroot/api/cache` that wraps every remote function with an **IndexedDB stale-while-revalidate cache**. On the first call the data is fetched from the server; on every subsequent call the cached data is returned instantly
|
|
15
|
+
The package ships a browser-side API at `@brownandroot/api/cache` that wraps every remote function with an **IndexedDB stale-while-revalidate cache**. On the first call the data is fetched from the server; on every subsequent call the cached data is returned instantly. A background refresh runs automatically when the cached data is older than **4 hours**.
|
|
16
16
|
|
|
17
17
|
All list and dropdown functions accept an optional filter object — every entity field is available as an optional filter key. The `q` field performs a case-insensitive partial-text match across the entity's searchable text fields.
|
|
18
18
|
|
package/dist/cache.js
CHANGED
|
@@ -18,13 +18,17 @@ import { getJobtypejobsteps as _getJobtypejobsteps, getJobtypejobstep as _getJob
|
|
|
18
18
|
// ---------------------------------------------------------------------------
|
|
19
19
|
// Internal: stale-while-revalidate fetch helpers
|
|
20
20
|
// ---------------------------------------------------------------------------
|
|
21
|
+
/** How long cached data is considered fresh before a background refresh is triggered. */
|
|
22
|
+
const CACHE_TTL_MS = 4 * 60 * 60 * 1000; // 4 hours
|
|
21
23
|
async function loadCached(store, fetcher) {
|
|
22
24
|
const cached = await idbGet(store, 'all');
|
|
23
25
|
if (cached) {
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
// Only refresh in the background if the cached data is older than the TTL
|
|
27
|
+
if (Date.now() - cached.fetchedAt >= CACHE_TTL_MS) {
|
|
28
|
+
fetcher()
|
|
29
|
+
.then((fresh) => idbSet(store, 'all', fresh))
|
|
30
|
+
.catch(() => { });
|
|
31
|
+
}
|
|
28
32
|
return cached.data;
|
|
29
33
|
}
|
|
30
34
|
// Cache miss — must await
|