@blaze-cache/swr 0.3.0 → 0.4.0-beta.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/README.md CHANGED
@@ -46,6 +46,10 @@ for that key) always wins over the cache.
46
46
  imperative `cache.get` / `cache.set` / `cache.delete`.
47
47
  * Successful fetches are written to the shared cache; failed fetches are not — errors
48
48
  stay in SWR, data crosses into Blaze.
49
+ * The cache layer never takes a fetch down with it: a hydration read that throws seeds
50
+ nothing (the hook fetches as plain `useSWR` would, with one console warning), and a
51
+ write that throws — or a `tags` resolver that does — returns the data unwritten,
52
+ warned and otherwise uninterrupted.
49
53
  * An `undefined` result is skipped too: SWR reads it as "no data", so there is nothing
50
54
  to stamp a TTL onto.
51
55
  * What is written is an **envelope** — `{ kind: 'swr', data, storedAt }` — and hydration
@@ -66,24 +70,25 @@ for that key) always wins over the cache.
66
70
 
67
71
  ## API
68
72
 
69
- Both entry points take `(cache, …, options?)` — the cache first, then the usual SWR
70
- arguments, then normal SWR options plus the Blaze-side stamp (`ttlMs`/`tags`).
73
+ The entry points take `(cache, …, options?)` — the cache first, then the usual SWR
74
+ arguments, then normal SWR options plus the Blaze-side stamp (`ttlMs`/`tags`/`forceRefresh`).
71
75
 
72
76
  | Export | What it does |
73
77
  | --- | --- |
74
78
  | `useBlazeSWR` | `useSWR` seeded from the cache, persisting successful fetches. |
75
79
  | `blazeSWRMiddleware` | The same behaviour as a plain SWR middleware, for `SWRConfig` or an existing `useSWR`. |
80
+ | `invalidateBlazeSWR` | Purge this package's entries by tag — foreign records on a shared cache survive — and revalidate the hooks that showed them. |
76
81
  | `resolveBlazeKey` | The key an SWR `key` is filed under in the Blaze Cache (`null` when paused). |
77
82
  | `readBlazeSWRRecord` | The gated hydration read: `key`'s data, but only when the value is this package's own envelope. |
78
83
  | `isBlazeSWRRecord` | The envelope shape check `readBlazeSWRRecord` uses. |
79
84
  | `DEFAULT_TTL_MS` / `DEFAULT_TAGS` | The write stamp used when nothing is given: 24 h, `['swr']`. |
80
- | `BlazeSWROptions` / `BlazeSWRMiddlewareOptions` / `BlazeSWRCacheLike` / `BlazeSWRRecord` | The option, cache and record types. |
85
+ | `BlazeSWROptions` / `BlazeSWRMiddlewareOptions` / `BlazeSWRCacheLike` / `BlazeSWRRecord` / `BlazeSWRTags` / `InvalidateBlazeSWROptions` | The option, cache, record and tag types. |
81
86
 
82
- Precedence for the write stamp: the current `useSWR`/`useBlazeSWR` options, then the
83
- middleware's own options, then the defaults. `useBlazeSWR` keeps `ttlMs`/`tags` in the
84
- SWR config it hands to `useSWR` rather than stripping them out, so a hook-level stamp
85
- also wins an `SWRConfig` provider's merged values — a provider default can never shadow
86
- a per-call one.
87
+ Precedence for the write stamp and `forceRefresh`: the current `useSWR`/`useBlazeSWR`
88
+ options, then the middleware's own options, then the defaults. `useBlazeSWR` keeps
89
+ `ttlMs`/`tags`/`forceRefresh` in the SWR config it hands to `useSWR` rather than
90
+ stripping them out, so a hook-level stamp also wins an `SWRConfig` provider's merged
91
+ values — a provider default can never shadow a per-call one.
87
92
 
88
93
  ## Example usage
89
94
 
@@ -110,6 +115,15 @@ is still read, nothing is written:
110
115
  const { data } = useBlazeSWR(cache, 'theme', null, { fallbackData: 'dark' });
111
116
  ```
112
117
 
118
+ `tags` may also be a function, resolved at write time from the key and the fetched data
119
+ — for tags only a successful fetch can be responsible for:
120
+
121
+ ```tsx
122
+ const { data } = useBlazeSWR(cache, '/api/users/7', fetchUser, {
123
+ tags: (key, data) => ['user', `user:${data.id}`],
124
+ });
125
+ ```
126
+
113
127
  ### `blazeSWRMiddleware` — one middleware for a whole tree
114
128
 
115
129
  ```tsx
@@ -130,14 +144,51 @@ const { data } = useSWR('/api/todos', fetcher, {
130
144
  });
131
145
  ```
132
146
 
147
+ ### `forceRefresh` — skip the paint, hit the network
148
+
149
+ ```tsx
150
+ const { data } = useBlazeSWR(cache, '/api/todos', fetchTodos, {
151
+ tags: ['todos'],
152
+ forceRefresh: true,
153
+ });
154
+ // no persisted seed — the fetcher runs immediately, and the result is still written
155
+ // back, so the next mount starts from it
156
+ ```
157
+
158
+ For a key that must be current *right now* — a pull-to-refresh, a view opened after a
159
+ mutation elsewhere: the persisted entry is not read, `revalidateOnMount` is forced so the
160
+ fetch runs even where this call or the provider would quiet it (the flag beats an
161
+ explicit `revalidateOnMount: false` — skipping the read is only honest if the network
162
+ runs), and the write still happens. Skip the read, keep the write — the same rule as
163
+ axios/fetch's `forceRefresh`. Caller `fallbackData` and provider `fallback` keep their
164
+ own precedence: they only stop *Blaze* from seeding, never the caller.
165
+
133
166
  ### Invalidating by tag
134
167
 
135
168
  ```ts
136
169
  // Writes carry tags…
137
170
  const { data } = useBlazeSWR(cache, '/api/todos', fetchTodos, { tags: ['todos'] });
138
171
 
139
- // …so a mutation that changes the list purges every persisted copy —
140
- // including ones from earlier sessions:
172
+ // …so a mutation that changes the list purges every persisted copy — including ones
173
+ // from earlier sessions — and revalidates the hooks that were showing the old one:
174
+ await invalidateBlazeSWR(cache, 'todos');
175
+ ```
176
+
177
+ `invalidateBlazeSWR` only ever touches this package's `{ kind: 'swr' }` envelopes: on a
178
+ cache shared with `@blaze-cache/axios` / `@blaze-cache/fetch` /
179
+ `@blaze-cache/react-query`, an entry another integration wrote — even one wearing the
180
+ same tag — survives. It returns the purged keys. Hooks under a *custom* `SWRConfig`
181
+ provider need that provider's mutator:
182
+
183
+ ```ts
184
+ const { mutate } = useSWRConfig();
185
+ await invalidateBlazeSWR(cache, 'todos', { mutate });
186
+ ```
187
+
188
+ The raw equivalent reaches the whole shared tag namespace and leaves SWR's in-memory
189
+ copy to you:
190
+
191
+ ```ts
141
192
  cache.invalidateTags('todos');
142
193
  mutate('/api/todos'); // SWR's own revalidation for the mounted hook
143
194
  ```
package/dist/index.d.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * SWR integration backed by Blaze Cache — stale-while-revalidate that survives a reload.
3
3
  *
4
- * Two entry points:
4
+ * Three entry points:
5
5
  *
6
6
  * * {@link useBlazeSWR} — `useSWR` that paints the persisted entry on the first render
7
- * and writes successful fetches back, with `ttlMs`/`tags` on top of normal SWR options.
7
+ * and writes successful fetches back, with `ttlMs`/`tags`/`forceRefresh` on top of
8
+ * normal SWR options.
8
9
  * * {@link blazeSWRMiddleware} — the same behaviour as a plain SWR middleware, for
9
10
  * wiring into `SWRConfig` or an existing `useSWR` call.
11
+ * * {@link invalidateBlazeSWR} — purge this package's entries by tag (without touching
12
+ * records another integration wrote under the same cache) and revalidate the hooks
13
+ * that showed them.
10
14
  *
11
15
  * {@link resolveBlazeKey} exposes the key an SWR `key` is filed under, for imperative
12
16
  * reads and writes (`cache.get(resolveBlazeKey(key))`).
@@ -26,8 +30,9 @@
26
30
  * @packageDocumentation
27
31
  */
28
32
  export { blazeSWRMiddleware, DEFAULT_TAGS, DEFAULT_TTL_MS } from './middleware.js';
33
+ export { invalidateBlazeSWR } from './invalidate.js';
29
34
  export { resolveBlazeKey } from './key.js';
30
35
  export { isBlazeSWRRecord, readBlazeSWRRecord } from './record.js';
31
36
  export { useBlazeSWR } from './useBlazeSWR.js';
32
- export type { BlazeSWRCacheLike, BlazeSWRRecord, BlazeSWROptions, BlazeSWRMiddlewareOptions, BlazeSWRStampOptions, } from './types.js';
37
+ export type { BlazeSWRCacheLike, BlazeSWRRecord, BlazeSWROptions, BlazeSWRMiddlewareOptions, BlazeSWRStampOptions, BlazeSWRTags, InvalidateBlazeSWROptions, } from './types.js';
33
38
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EACV,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,yBAAyB,EACzB,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EACV,iBAAiB,EACjB,cAAc,EACd,eAAe,EACf,yBAAyB,EACzB,oBAAoB,EACpB,YAAY,EACZ,yBAAyB,GAC1B,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,12 +1,16 @@
1
1
  /**
2
2
  * SWR integration backed by Blaze Cache — stale-while-revalidate that survives a reload.
3
3
  *
4
- * Two entry points:
4
+ * Three entry points:
5
5
  *
6
6
  * * {@link useBlazeSWR} — `useSWR` that paints the persisted entry on the first render
7
- * and writes successful fetches back, with `ttlMs`/`tags` on top of normal SWR options.
7
+ * and writes successful fetches back, with `ttlMs`/`tags`/`forceRefresh` on top of
8
+ * normal SWR options.
8
9
  * * {@link blazeSWRMiddleware} — the same behaviour as a plain SWR middleware, for
9
10
  * wiring into `SWRConfig` or an existing `useSWR` call.
11
+ * * {@link invalidateBlazeSWR} — purge this package's entries by tag (without touching
12
+ * records another integration wrote under the same cache) and revalidate the hooks
13
+ * that showed them.
10
14
  *
11
15
  * {@link resolveBlazeKey} exposes the key an SWR `key` is filed under, for imperative
12
16
  * reads and writes (`cache.get(resolveBlazeKey(key))`).
@@ -26,6 +30,7 @@
26
30
  * @packageDocumentation
27
31
  */
28
32
  export { blazeSWRMiddleware, DEFAULT_TAGS, DEFAULT_TTL_MS } from './middleware.js';
33
+ export { invalidateBlazeSWR } from './invalidate.js';
29
34
  export { resolveBlazeKey } from './key.js';
30
35
  export { isBlazeSWRRecord, readBlazeSWRRecord } from './record.js';
31
36
  export { useBlazeSWR } from './useBlazeSWR.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACnF,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { BlazeSWRCacheLike, InvalidateBlazeSWROptions } from './types.js';
2
+ /**
3
+ * Purges this package's persisted entries by tag — and nothing else — then tells SWR.
4
+ *
5
+ * ```ts
6
+ * await invalidateBlazeSWR(cache, 'user'); // every entry tagged 'user'
7
+ * await invalidateBlazeSWR(cache, 'user', 'todos'); // either tag
8
+ * await invalidateBlazeSWR(cache); // every entry this package wrote
9
+ * ```
10
+ *
11
+ * Scoped the way the react-query integration scopes its mirror purge: every candidate is
12
+ * checked for *this package's* `{ kind: 'swr' }` envelope before it is touched, so on a
13
+ * cache shared with axios/fetch/react-query an entry another integration wrote — even
14
+ * one wearing the same tag — survives. (`cache.invalidateTags(...)` is the global
15
+ * equivalent: it reaches the whole tag namespace, all integrations included.)
16
+ *
17
+ * SWR's own view stays coherent: each purged key is handed to `mutate`, which
18
+ * revalidates its mounted hooks instead of letting them keep data the cache no longer
19
+ * holds. Unmounted hooks need nothing — their next mount hydrates from the now-empty
20
+ * key and fetches as usual.
21
+ *
22
+ * @param cache The Blaze Cache (or anything {@link BlazeSWRCacheLike}-shaped with `entries()`).
23
+ * @param tags Tags to purge; none given purges every entry this package wrote.
24
+ * @param options `mutate` — how to reach SWR; defaults to `swr`'s global mutator (pass
25
+ * `useSWRConfig().mutate` for hooks under a custom provider).
26
+ * @returns The purged keys, in enumeration order.
27
+ * @throws When the cache cannot be enumerated (`entries()`) or cannot delete — a partial
28
+ * purge on a shared cache is exactly the accident this helper exists to prevent.
29
+ */
30
+ export declare function invalidateBlazeSWR(cache: BlazeSWRCacheLike, ...tags: string[]): Promise<string[]>;
31
+ export declare function invalidateBlazeSWR(cache: BlazeSWRCacheLike, ...args: [...tags: string[], options: InvalidateBlazeSWROptions]): Promise<string[]>;
32
+ //# sourceMappingURL=invalidate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalidate.d.ts","sourceRoot":"","sources":["../src/invalidate.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAqB/E;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,iBAAiB,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;AACnG,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,iBAAiB,EACxB,GAAG,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,yBAAyB,CAAC,GAC/D,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC"}
@@ -0,0 +1,50 @@
1
+ import { mutate as globalMutate } from 'swr';
2
+ import { isBlazeSWRRecord } from './record.js';
3
+ /**
4
+ * Every live entry, or a refusal: enumerating is how this purge stays scoped to *this
5
+ * package's* records, so a cache that cannot enumerate gets an error instead of a
6
+ * silently partial purge — the mirror-side helpers refuse the same way.
7
+ */
8
+ function entriesOf(cache) {
9
+ if (typeof cache.entries !== 'function') {
10
+ throw new Error('[blaze-cache/swr] `invalidateBlazeSWR` selects this package’s records from every ' +
11
+ 'persisted entry, but the cache does not implement `entries()`. Refusing to run a ' +
12
+ 'deliberately partial purge — use the concrete `BlazeCache` from ' +
13
+ '`@blaze-cache/core`.');
14
+ }
15
+ return cache.entries();
16
+ }
17
+ export async function invalidateBlazeSWR(cache, ...args) {
18
+ // A trailing object is the options bag; everything before it — or everything, when
19
+ // there is none — is a tag.
20
+ const last = args[args.length - 1];
21
+ const options = last !== null && typeof last === 'object' ? last : undefined;
22
+ const tags = (options === undefined ? args : args.slice(0, -1));
23
+ // Shape-gated selection: only this package's envelope, only under a requested tag (or
24
+ // every envelope when no tag is given) — foreign records sharing the cache survive.
25
+ const removed = [];
26
+ for (const entry of entriesOf(cache)) {
27
+ if (!isBlazeSWRRecord(entry.value))
28
+ continue;
29
+ if (tags.length > 0 && !entry.tags.some((tag) => tags.includes(tag)))
30
+ continue;
31
+ removed.push(entry.key);
32
+ }
33
+ if (removed.length > 0) {
34
+ // Called through the cache, never detached — `BlazeCache.delete` touches private
35
+ // state, so a borrowed method would lose its receiver.
36
+ if (typeof cache.delete !== 'function') {
37
+ throw new Error('[blaze-cache/swr] `invalidateBlazeSWR` found entries to purge, but the cache ' +
38
+ 'does not implement `delete()`. Nothing was removed — use the concrete ' +
39
+ '`BlazeCache` from `@blaze-cache/core`.');
40
+ }
41
+ for (const key of removed)
42
+ cache.delete(key);
43
+ }
44
+ // Revalidate what was just purged: `mutate(key)` with no data only triggers
45
+ // revalidation (it never clears state), and with no mounted hook it is a no-op.
46
+ const notify = options?.mutate ?? globalMutate;
47
+ await Promise.all(removed.map((key) => notify(key)));
48
+ return removed;
49
+ }
50
+ //# sourceMappingURL=invalidate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invalidate.js","sourceRoot":"","sources":["../src/invalidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,KAAK,CAAC;AAE7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C;;;;GAIG;AACH,SAAS,SAAS,CAChB,KAAwB;IAExB,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CACb,mFAAmF;YACjF,mFAAmF;YACnF,kEAAkE;YAClE,sBAAsB,CACzB,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;AACzB,CAAC;AAmCD,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,KAAwB,EACxB,GAAG,IAA+C;IAElD,mFAAmF;IACnF,4BAA4B;IAC5B,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7E,MAAM,IAAI,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,I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still applies), so the fetcher runs in the background exactly like plain `useSWR` |
15
15
  * | fetch success | the result is written to the Blaze Cache under the SWR-serialized key, stamped with `ttlMs`/`tags` |
16
16
  * | fetch failure | ignored by the cache — errors live in SWR, data lives in Blaze |
17
+ * | cache failure | never fatal — a read that throws seeds nothing (warned once), a write that throws returns the data unwritten (warned per write) |
17
18
  *
18
19
  * The seeded entry is deliberately **not** treated as fresh: SWR's `fallbackData` carries
19
20
  * no timestamp, so `revalidateIfStale` (on by default) still revalidates on mount. Set
20
21
  * `revalidateOnMount: false` or `revalidateIfStale: false` when a cache hit should skip
21
22
  * the network entirely.
22
23
  *
24
+ * `forceRefresh: true` (per call, or once on the middleware) inverts the first row: the
25
+ * persisted entry is not read at all, and `revalidateOnMount` is forced to `true` so the
26
+ * fetcher runs regardless of what this call or the provider would otherwise decide —
27
+ * while the write still happens, so the next mount starts from the fresh result.
28
+ *
23
29
  * Precedence for what wins the first paint: a caller-supplied `fallbackData`, then an
24
30
  * entry in a provider-level `fallback` map, then the Blaze Cache. Precedence for the
25
- * write stamp: `ttlMs`/`tags` on the current options, then the middleware's own
26
- * options, then {@link DEFAULT_TTL_MS}/{@link DEFAULT_TAGS}.
31
+ * write stamp and `forceRefresh`: the current call's options, then the middleware's own
32
+ * options, then {@link DEFAULT_TTL_MS}/{@link DEFAULT_TAGS}/no forced refresh.
27
33
  *
28
34
  * A paused key (`null`/`undefined`/`false`, or a lazy key resolving to one) reads and
29
35
  * writes nothing — {@link resolveBlazeKey} maps those to `null` and the middleware passes
@@ -52,5 +58,5 @@ export declare const DEFAULT_TAGS: readonly string[];
52
58
  * @param cache The Blaze Cache (or anything {@link BlazeSWRCacheLike}-shaped).
53
59
  * @param options The Blaze-side stamp for writes; per-call options still override it.
54
60
  */
55
- export declare function blazeSWRMiddleware(cache: BlazeSWRCacheLike, options?: BlazeSWRMiddlewareOptions): Middleware;
61
+ export declare function blazeSWRMiddleware<Data = unknown>(cache: BlazeSWRCacheLike, options?: BlazeSWRMiddlewareOptions<Data>): Middleware;
56
62
  //# sourceMappingURL=middleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,UAAU,EAGX,MAAM,KAAK,CAAC;AAIb,OAAO,KAAK,EACV,iBAAiB,EACjB,yBAAyB,EAE1B,MAAM,YAAY,CAAC;AAEpB,2FAA2F;AAC3F,eAAO,MAAM,cAAc,WAAa,CAAC;AACzC,0FAA0F;AAC1F,eAAO,MAAM,YAAY,EAAE,SAAS,MAAM,EAAY,CAAC;AAuCvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,iBAAiB,EACxB,OAAO,GAAE,yBAA8B,GACtC,UAAU,CAyCZ"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,UAAU,EAGX,MAAM,KAAK,CAAC;AAIb,OAAO,KAAK,EACV,iBAAiB,EACjB,yBAAyB,EAG1B,MAAM,YAAY,CAAC;AAEpB,2FAA2F;AAC3F,eAAO,MAAM,cAAc,WAAa,CAAC;AACzC,0FAA0F;AAC1F,eAAO,MAAM,YAAY,EAAE,SAAS,MAAM,EAAY,CAAC;AAuDvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,GAAG,OAAO,EAC/C,KAAK,EAAE,iBAAiB,EACxB,OAAO,GAAE,yBAAyB,CAAC,IAAI,CAAM,GAC5C,UAAU,CA6CZ"}
@@ -12,19 +12,31 @@ export const DEFAULT_TAGS = ['swr'];
12
12
  * the key happens at the call site: inside a captured closure TypeScript would have to
13
13
  * assume the parameter changed since the null check.
14
14
  *
15
- * Failures propagate untouched — SWR keeps its own error state, and the cache only ever
16
- * holds data that a fetcher actually returned. `undefined` is skipped: SWR reads it as
17
- * "no data", so there is nothing worth stamping a TTL onto. The raw result goes into a
15
+ * Fetch failures propagate untouched — SWR keeps its own error state, and the cache only
16
+ * ever holds data that a fetcher actually returned. `undefined` is skipped: SWR reads it
17
+ * as "no data", so there is nothing worth stamping a TTL onto. The raw result goes into a
18
18
  * {@link BlazeSWRRecord} envelope, so hydration can tell this package's writes apart from
19
19
  * foreign values on a shared cache (see {@link readBlazeSWRRecord}); the fetcher itself
20
20
  * still resolves with the bare result, exactly as the caller's fetcher would.
21
+ *
22
+ * Cache-side failures are guarded the other way round: a `tags` resolver or a `cache.set`
23
+ * that throws cannot take the fetched data with it — the write is skipped, one warning
24
+ * says so, and the result resolves as if the cache were never there.
21
25
  */
22
26
  function persistingFetcher(fetcher, cache, key, stamp) {
23
27
  return async (...args) => {
24
28
  const data = await fetcher(...args);
25
29
  if (data !== undefined) {
26
- const record = { kind: 'swr', data, storedAt: Date.now() };
27
- cache.set(key, record, stamp);
30
+ try {
31
+ // A `tags` resolver gets the same argument the fetcher just received — the
32
+ // resolved key, never the hash it is filed under.
33
+ const tags = typeof stamp.tags === 'function' ? stamp.tags(args[0], data) : stamp.tags;
34
+ const record = { kind: 'swr', data, storedAt: Date.now() };
35
+ cache.set(key, record, { ttlMs: stamp.ttlMs, tags });
36
+ }
37
+ catch (error) {
38
+ console.warn('[blaze-cache] storing the response failed — it is returned unstored', error);
39
+ }
28
40
  }
29
41
  return data;
30
42
  };
@@ -39,16 +51,22 @@ function persistingFetcher(fetcher, cache, key, stamp) {
39
51
  * | mount / revalidate | SWR runs its own revalidation as usual (`revalidateIfStale` still applies), so the fetcher runs in the background exactly like plain `useSWR` |
40
52
  * | fetch success | the result is written to the Blaze Cache under the SWR-serialized key, stamped with `ttlMs`/`tags` |
41
53
  * | fetch failure | ignored by the cache — errors live in SWR, data lives in Blaze |
54
+ * | cache failure | never fatal — a read that throws seeds nothing (warned once), a write that throws returns the data unwritten (warned per write) |
42
55
  *
43
56
  * The seeded entry is deliberately **not** treated as fresh: SWR's `fallbackData` carries
44
57
  * no timestamp, so `revalidateIfStale` (on by default) still revalidates on mount. Set
45
58
  * `revalidateOnMount: false` or `revalidateIfStale: false` when a cache hit should skip
46
59
  * the network entirely.
47
60
  *
61
+ * `forceRefresh: true` (per call, or once on the middleware) inverts the first row: the
62
+ * persisted entry is not read at all, and `revalidateOnMount` is forced to `true` so the
63
+ * fetcher runs regardless of what this call or the provider would otherwise decide —
64
+ * while the write still happens, so the next mount starts from the fresh result.
65
+ *
48
66
  * Precedence for what wins the first paint: a caller-supplied `fallbackData`, then an
49
67
  * entry in a provider-level `fallback` map, then the Blaze Cache. Precedence for the
50
- * write stamp: `ttlMs`/`tags` on the current options, then the middleware's own
51
- * options, then {@link DEFAULT_TTL_MS}/{@link DEFAULT_TAGS}.
68
+ * write stamp and `forceRefresh`: the current call's options, then the middleware's own
69
+ * options, then {@link DEFAULT_TTL_MS}/{@link DEFAULT_TAGS}/no forced refresh.
52
70
  *
53
71
  * A paused key (`null`/`undefined`/`false`, or a lazy key resolving to one) reads and
54
72
  * writes nothing — {@link resolveBlazeKey} maps those to `null` and the middleware passes
@@ -80,28 +98,33 @@ function persistingFetcher(fetcher, cache, key, stamp) {
80
98
  export function blazeSWRMiddleware(cache, options = {}) {
81
99
  return (useSWRNext) => (key, fetcher, config) => {
82
100
  const blazeKey = resolveBlazeKey(key);
83
- // Per-call `ttlMs`/`tags` ride in the SWR config (SWR merges unknown options through),
84
- // then the middleware's own options, then the package defaults.
101
+ // Per-call `ttlMs`/`tags`/`forceRefresh` ride in the SWR config (SWR merges unknown
102
+ // options through), then the middleware's own options, then the package defaults.
103
+ const callOptions = config;
85
104
  const stamp = {
86
- ttlMs: config.ttlMs ??
87
- options.ttlMs ??
88
- DEFAULT_TTL_MS,
89
- tags: config.tags ??
90
- options.tags ??
91
- DEFAULT_TAGS,
105
+ ttlMs: callOptions.ttlMs ?? options.ttlMs ?? DEFAULT_TTL_MS,
106
+ tags: callOptions.tags ?? options.tags ?? DEFAULT_TAGS,
92
107
  };
108
+ const forceRefresh = callOptions.forceRefresh ?? options.forceRefresh ?? false;
93
109
  // Hydration: `peek` first so renders never move the hit rate or nudge eviction —
94
110
  // a recording `get` stays the fallback for caches that predate it. Only this
95
111
  // package's own envelope seeds the UI; a foreign value under a colliding key reads
96
- // as a miss rather than being handed to the component as `data`.
97
- const data = blazeKey === null ? undefined : readBlazeSWRRecord(cache, blazeKey);
112
+ // as a miss rather than being handed to the component as `data`. `forceRefresh`
113
+ // skips the read entirely — the persisted entry must not quiet this mount.
114
+ const data = blazeKey === null || forceRefresh ? undefined : readBlazeSWRRecord(cache, blazeKey);
98
115
  // Caller-supplied fallbacks stay authoritative: `fallbackData` directly, or a
99
116
  // provider-level `fallback` entry for this exact key. Blaze only fills the gap.
100
117
  const callerFallback = config.fallbackData !== undefined ||
101
118
  (blazeKey !== null && config.fallback?.[blazeKey] !== undefined);
119
+ // A forced refresh has to actually fetch: with nothing seeded, SWR needs
120
+ // `revalidateOnMount` — and it must win even over an explicit `false` (from this call
121
+ // or the provider), because skipping the read is only honest if the network runs. The
122
+ // branches cannot overlap: `data` is undefined whenever `forceRefresh` is set.
102
123
  const nextConfig = data !== undefined && !callerFallback
103
124
  ? { ...config, fallbackData: data }
104
- : config;
125
+ : forceRefresh && blazeKey !== null
126
+ ? { ...config, revalidateOnMount: true }
127
+ : config;
105
128
  const nextFetcher = fetcher !== null && blazeKey !== null
106
129
  ? persistingFetcher(fetcher, cache, blazeKey, stamp)
107
130
  : fetcher;
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAOjD,2FAA2F;AAC3F,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC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Y,IAAI,OAAO,CAAC,YAAY,IAAI,KAAK,CAAC;QAE/E,iFAAiF;QACjF,6EAA6E;QAC7E,mFAAmF;QACnF,gFAAgF;QAChF,2EAA2E;QAC3E,MAAM,IAAI,GACR,QAAQ,KAAK,IAAI,IAAI,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,kBAAkB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAEtF,8EAA8E;QAC9E,gFAAgF;QAChF,MAAM,cAAc,GAClB,MAAM,CAAC,YAAY,KAAK,SAAS;YACjC,CAAC,QAAQ,KAAK,IAAI,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,KAAK,SAAS,CAAC,CAAC;QAEnE,yEAAyE;QACzE,sFAAsF;QACtF,sFAAsF;QACtF,+EAA+E;QAC/E,MAAM,UAAU,GACd,IAAI,KAAK,SAAS,IAAI,CAAC,cAAc;YACnC,CAAC,CAAE,EAAE,GAAG,MAAM,EAAE,YAAY,EAAE,IAAI,EAAuB;YACzD,CAAC,CAAC,YAAY,IAAI,QAAQ,KAAK,IAAI;gBACjC,CAAC,CAAE,EAAE,GAAG,MAAM,EAAE,iBAAiB,EAAE,IAAI,EAAuB;gBAC9D,CAAC,CAAC,MAAM,CAAC;QAEf,MAAM,WAAW,GACf,OAAO,KAAK,IAAI,IAAI,QAAQ,KAAK,IAAI;YACnC,CAAC,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC;YACpD,CAAC,CAAC,OAAO,CAAC;QAEd,OAAO,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,UAAU,CAAgB,CAAC;IACjE,CAAC,CAAC;AACJ,CAAC"}
package/dist/record.d.ts CHANGED
@@ -11,7 +11,9 @@ import type { BlazeSWRCacheLike, BlazeSWRRecord } from './types.js';
11
11
  * component as `data`.
12
12
  *
13
13
  * Read with `peek` when the cache has one — a render must not move the hit rate or nudge
14
- * eviction — falling back to `get` for caches that predate it.
14
+ * eviction — falling back to `get` for caches that predate it. A cache that *throws* here
15
+ * reads as the miss it then is: the hook fetches as plain `useSWR` would, the render
16
+ * lives, and one console warning says so.
15
17
  */
16
18
  export declare function readBlazeSWRRecord<Data = unknown>(cache: BlazeSWRCacheLike, key: string): Data | undefined;
17
19
  /** True when `value` is the envelope this package writes. */
@@ -1 +1 @@
1
- {"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,GAAG,OAAO,EAC/C,KAAK,EAAE,iBAAiB,EACxB,GAAG,EAAE,MAAM,GACV,IAAI,GAAG,SAAS,CAGlB;AAED,6DAA6D;AAC7D,wBAAgB,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAAC,CAAC,CAAC,CAOxF"}
1
+ {"version":3,"file":"record.d.ts","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAKpE;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,GAAG,OAAO,EAC/C,KAAK,EAAE,iBAAiB,EACxB,GAAG,EAAE,MAAM,GACV,IAAI,GAAG,SAAS,CAYlB;AAED,6DAA6D;AAC7D,wBAAgB,gBAAgB,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,cAAc,CAAC,CAAC,CAAC,CAOxF"}
package/dist/record.js CHANGED
@@ -1,3 +1,5 @@
1
+ /** One warning for the life of the module — the read runs on every render. */
2
+ let warned = false;
1
3
  /**
2
4
  * Reads `key` and returns the record's data **only when the value is one this package
3
5
  * wrote** — the hydration gate.
@@ -10,10 +12,22 @@
10
12
  * component as `data`.
11
13
  *
12
14
  * Read with `peek` when the cache has one — a render must not move the hit rate or nudge
13
- * eviction — falling back to `get` for caches that predate it.
15
+ * eviction — falling back to `get` for caches that predate it. A cache that *throws* here
16
+ * reads as the miss it then is: the hook fetches as plain `useSWR` would, the render
17
+ * lives, and one console warning says so.
14
18
  */
15
19
  export function readBlazeSWRRecord(cache, key) {
16
- const value = cache.peek ? cache.peek(key) : cache.get(key);
20
+ let value;
21
+ try {
22
+ value = cache.peek ? cache.peek(key) : cache.get(key);
23
+ }
24
+ catch (error) {
25
+ if (!warned) {
26
+ warned = true;
27
+ console.warn('[blaze-cache] reading from the cache failed — hydrating without it', error);
28
+ }
29
+ return undefined;
30
+ }
17
31
  return isBlazeSWRRecord(value) ? value.data : undefined;
18
32
  }
19
33
  /** True when `value` is the envelope this package writes. */
@@ -1 +1 @@
1
- {"version":3,"file":"record.js","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAwB,EACxB,GAAW;IAEX,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5D,OAAO,gBAAgB,CAAO,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AAChE,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,gBAAgB,CAAc,KAAc;IAC1D,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,KAAK;QAC5C,OAAQ,KAAgC,CAAC,QAAQ,KAAK,QAAQ,CAC/D,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"record.js","sourceRoot":"","sources":["../src/record.ts"],"names":[],"mappings":"AAEA,8EAA8E;AAC9E,IAAI,MAAM,GAAG,KAAK,CAAC;AAEnB;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAwB,EACxB,GAAW;IAEX,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,GAAG,IAAI,CAAC;YACd,OAAO,CAAC,IAAI,CAAC,oEAAoE,EAAE,KAAK,CAAC,CAAC;QAC5F,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,gBAAgB,CAAO,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AAChE,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,gBAAgB,CAAc,KAAc;IAC1D,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,KAAK;QAC5C,OAAQ,KAAgC,CAAC,QAAQ,KAAK,QAAQ,CAC/D,CAAC;AACJ,CAAC"}
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { BareFetcher, SWRConfiguration } from 'swr';
1
+ import type { Arguments, BareFetcher, ScopedMutator, SWRConfiguration } from 'swr';
2
2
  /**
3
3
  * What this package writes to the cache: the fetcher's result wrapped in a self-describing
4
4
  * envelope.
@@ -21,6 +21,17 @@ export interface BlazeSWRRecord<T = unknown> {
21
21
  /** Wall-clock time of the write. */
22
22
  storedAt: number;
23
23
  }
24
+ /**
25
+ * The `tags` option: a fixed list, or a resolver applied **at write time** from the key
26
+ * and the fetched result — for tags only a successful fetch can be responsible for
27
+ * (`(key, data) => ['user', \`user:${data.id}\`]`).
28
+ *
29
+ * The resolver runs inside the write's guard: one that throws skips the write (with a
30
+ * warning), exactly like a `cache.set` that throws — the fetched data itself always
31
+ * resolves untouched. `key` is what the fetcher receives (a lazy `() => key` already
32
+ * evaluated), never the serialized hash the entry is filed under.
33
+ */
34
+ export type BlazeSWRTags<Data = unknown> = readonly string[] | ((key: Arguments, data: Data) => readonly string[]);
24
35
  /**
25
36
  * The slice of a cache the SWR integration needs.
26
37
  *
@@ -54,34 +65,73 @@ export interface BlazeSWRCacheLike<V = any> {
54
65
  tags?: readonly string[];
55
66
  }): void;
56
67
  delete?(key: string): boolean;
68
+ /**
69
+ * Every live entry — key, value, TTL and tags — without touching statistics.
70
+ *
71
+ * Optional, like {@link BlazeSWRCacheLike.peek}: only {@link invalidateBlazeSWR} needs
72
+ * it, to select *this package's* records on a cache that may be shared — an SWR key can
73
+ * point at another integration's record, and a purge must never delete that. A cache
74
+ * without it makes that helper **throw** rather than run a deliberately partial purge;
75
+ * the concrete `BlazeCache` has it as `entries()`.
76
+ */
77
+ entries?(): Iterable<{
78
+ key: string;
79
+ value: V;
80
+ ttlMs: number | null;
81
+ tags: string[];
82
+ }>;
57
83
  /** Removes every entry carrying any of `tags`, returning the removed keys. */
58
84
  invalidateTags?(...tags: string[]): string[];
59
85
  }
60
86
  /**
61
- * How long a written entry survives in the Blaze Cache, and under which tags — the
62
- * `cache.set` stamp, both optional.
87
+ * How long a written entry survives in the Blaze Cache, under which tags, and whether a
88
+ * mount may read it at all — the `cache.set` stamp plus the hydration switch, all optional.
63
89
  *
64
90
  * Read from three places, in order of precedence: the options passed to the current
65
91
  * `useSWR`/`useBlazeSWR` call, the options given to {@link blazeSWRMiddleware}, then the
66
- * package defaults (24 h, tag `'swr'`).
92
+ * package defaults (24 h, tag `'swr'`, no forced refresh).
67
93
  */
68
- export interface BlazeSWRStampOptions {
94
+ export interface BlazeSWRStampOptions<Data = unknown> {
69
95
  /** TTL for the entry in milliseconds. Defaults to 24 h. */
70
96
  ttlMs?: number;
71
- /** Tags attached to the entry — enables `cache.invalidateTags('todos')`. */
72
- tags?: readonly string[];
97
+ /** Tags attached to the entry — enables `cache.invalidateTags('todos')`. See {@link BlazeSWRTags}. */
98
+ tags?: BlazeSWRTags<Data>;
99
+ /**
100
+ * Skip the persisted entry for this key and fetch anyway: no Blaze hydration, and
101
+ * `revalidateOnMount` forced to `true` — winning over an explicit `false`, because the
102
+ * flag's whole job is to guarantee the fetcher runs. The result is still written back,
103
+ * same as axios/fetch's `forceRefresh`: skip the read, keep the write.
104
+ *
105
+ * Hook-level beats middleware-level. Caller `fallbackData`/provider `fallback` keep
106
+ * their own precedence — they are never overridden; they only stop *Blaze* from seeding.
107
+ */
108
+ forceRefresh?: boolean;
109
+ }
110
+ /** Options {@link blazeSWRMiddleware} accepts — the Blaze-side stamp plus `forceRefresh`. */
111
+ export type BlazeSWRMiddlewareOptions<Data = unknown> = BlazeSWRStampOptions<Data>;
112
+ /**
113
+ * Options for {@link invalidateBlazeSWR} — just how to reach SWR's own cache.
114
+ */
115
+ export interface InvalidateBlazeSWROptions {
116
+ /**
117
+ * The `mutate` that revalidates each purged key's mounted hooks, so the UI stops
118
+ * showing data the cache no longer holds.
119
+ *
120
+ * Defaults to `swr`'s global `mutate`, which reaches hooks under the default provider.
121
+ * Hooks under a *custom* `SWRConfig` provider need that provider's mutate — take it
122
+ * from `useSWRConfig()`.
123
+ */
124
+ mutate?: ScopedMutator;
73
125
  }
74
- /** Options {@link blazeSWRMiddleware} accepts — just the Blaze-side stamp. */
75
- export type BlazeSWRMiddlewareOptions = BlazeSWRStampOptions;
76
126
  /**
77
- * Normal SWR options plus the Blaze Cache stamp (`ttlMs`/`tags`) — everything
78
- * `useSWR` accepts keeps working; the two extra fields only shape how successful
79
- * fetches are written to the Blaze Cache.
127
+ * Normal SWR options plus the Blaze Cache stamp (`ttlMs`/`tags`/`forceRefresh`) — everything
128
+ * `useSWR` accepts keeps working; the extra fields only shape how successful fetches are
129
+ * written to the Blaze Cache and whether a mount may read one.
80
130
  *
81
131
  * They may also be set once for every hook under an `SWRConfig` provider (cast to
82
132
  * `BlazeSWRStampOptions` — SWR merges unknown options into the config the middleware
83
133
  * receives), or passed straight to {@link blazeSWRMiddleware} when you wire it up by hand.
84
134
  */
85
- export interface BlazeSWROptions<Data = any, Error = any, SWRFetcher extends BareFetcher<any> = BareFetcher<Data>> extends SWRConfiguration<Data, Error, SWRFetcher>, BlazeSWRStampOptions {
135
+ export interface BlazeSWROptions<Data = any, Error = any, SWRFetcher extends BareFetcher<any> = BareFetcher<Data>> extends SWRConfiguration<Data, Error, SWRFetcher>, BlazeSWRStampOptions<Data> {
86
136
  }
87
137
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,KAAK,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,OAAO;IACzC,kEAAkE;IAClE,IAAI,EAAE,KAAK,CAAC;IACZ,iDAAiD;IACjD,IAAI,EAAE,CAAC,CAAC;IACR,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,GAAG;IACxC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;IAChC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;IAClC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EA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AM,WAAW,yBAAyB;IACxC;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,eAAe,CAC9B,IAAI,GAAG,GAAG,EACV,KAAK,GAAG,GAAG,EACX,UAAU,SAAS,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CACvD,SAAQ,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,UAAU,CAAC,EAC/C,oBAAoB,CAAC,IAAI,CAAC;CAAG"}
@@ -23,23 +23,28 @@ import type { BlazeSWRCacheLike, BlazeSWROptions } from './types.js';
23
23
  * It governs *cross-session* freshness; SWR's own options keep governing *in-session*
24
24
  * freshness.
25
25
  * * `tags` — attached to every write, so `cache.invalidateTags('todos')` purges them.
26
+ * A function is resolved at write time from the key and the fetched data:
27
+ * `tags: (key, data) => ['user', \`user:${data.id}\`]`.
28
+ * * `forceRefresh` — skip the persisted entry and fetch anyway; the result is still
29
+ * written back, so the next mount starts from it.
26
30
  *
27
31
  * Any other middleware in `options.use` composes normally; this one is placed first so
28
32
  * it seeds the config and wraps the fetcher before they transform either.
29
33
  *
30
- * Invalidating by tag (the fetcher runs again on the next mount; SWR's in-memory copy
31
- * keeps its own lifecycle, so revalidate it too when it must change on the spot):
34
+ * Invalidating by tag — `invalidateBlazeSWR` purges every entry this package wrote under
35
+ * the tag *and* revalidates the mounted hooks in one call (`cache.invalidateTags` is the
36
+ * raw equivalent: it reaches the whole shared tag namespace, and leaves SWR's in-memory
37
+ * copy to you):
32
38
  *
33
39
  * ```ts
34
- * cache.invalidateTags('todos');
35
- * mutate('/api/todos'); // SWR's own revalidation for the mounted hook
40
+ * await invalidateBlazeSWR(cache, 'todos');
36
41
  * ```
37
42
  *
38
43
  * @param cache The Blaze Cache (or anything {@link BlazeSWRCacheLike}-shaped).
39
44
  * @param key The SWR key — string, tuple, object, or a lazy function; `null` pauses.
40
45
  * @param fetcher The fetcher; omit it (or pass `null`) to use a global one or for
41
46
  * client-side state — the cache is still read, nothing is written.
42
- * @param options Normal SWR options plus `ttlMs`/`tags`.
47
+ * @param options Normal SWR options plus `ttlMs`/`tags`/`forceRefresh`.
43
48
  */
44
49
  export declare function useBlazeSWR<Data = any, Error = any, SWRKey extends Key = Key>(cache: BlazeSWRCacheLike, key: SWRKey, fetcher?: Fetcher<Data, SWRKey> | null, options?: BlazeSWROptions<Data, Error, Fetcher<Data, SWRKey>>): SWRResponse<Data, Error>;
45
50
  //# sourceMappingURL=useBlazeSWR.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useBlazeSWR.d.ts","sourceRoot":"","sources":["../src/useBlazeSWR.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,WAAW,CACzB,IAAI,GAAG,GAAG,EACV,KAAK,GAAG,GAAG,EACX,MAAM,SAAS,GAAG,GAAG,GAAG,EAExB,KAAK,EAAE,iBAAiB,EACxB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,IAAW,EAC5C,OAAO,GAAE,eAAe,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAM,GAChE,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAY1B"}
1
+ {"version":3,"file":"useBlazeSWR.d.ts","sourceRoot":"","sources":["../src/useBlazeSWR.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAGrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAgB,WAAW,CACzB,IAAI,GAAG,GAAG,EACV,KAAK,GAAG,GAAG,EACX,MAAM,SAAS,GAAG,GAAG,GAAG,EAExB,KAAK,EAAE,iBAAiB,EACxB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,IAAW,EAC5C,OAAO,GAAE,eAAe,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAM,GAChE,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAoB1B"}
@@ -23,34 +23,47 @@ import { blazeSWRMiddleware } from './middleware.js';
23
23
  * It governs *cross-session* freshness; SWR's own options keep governing *in-session*
24
24
  * freshness.
25
25
  * * `tags` — attached to every write, so `cache.invalidateTags('todos')` purges them.
26
+ * A function is resolved at write time from the key and the fetched data:
27
+ * `tags: (key, data) => ['user', \`user:${data.id}\`]`.
28
+ * * `forceRefresh` — skip the persisted entry and fetch anyway; the result is still
29
+ * written back, so the next mount starts from it.
26
30
  *
27
31
  * Any other middleware in `options.use` composes normally; this one is placed first so
28
32
  * it seeds the config and wraps the fetcher before they transform either.
29
33
  *
30
- * Invalidating by tag (the fetcher runs again on the next mount; SWR's in-memory copy
31
- * keeps its own lifecycle, so revalidate it too when it must change on the spot):
34
+ * Invalidating by tag — `invalidateBlazeSWR` purges every entry this package wrote under
35
+ * the tag *and* revalidates the mounted hooks in one call (`cache.invalidateTags` is the
36
+ * raw equivalent: it reaches the whole shared tag namespace, and leaves SWR's in-memory
37
+ * copy to you):
32
38
  *
33
39
  * ```ts
34
- * cache.invalidateTags('todos');
35
- * mutate('/api/todos'); // SWR's own revalidation for the mounted hook
40
+ * await invalidateBlazeSWR(cache, 'todos');
36
41
  * ```
37
42
  *
38
43
  * @param cache The Blaze Cache (or anything {@link BlazeSWRCacheLike}-shaped).
39
44
  * @param key The SWR key — string, tuple, object, or a lazy function; `null` pauses.
40
45
  * @param fetcher The fetcher; omit it (or pass `null`) to use a global one or for
41
46
  * client-side state — the cache is still read, nothing is written.
42
- * @param options Normal SWR options plus `ttlMs`/`tags`.
47
+ * @param options Normal SWR options plus `ttlMs`/`tags`/`forceRefresh`.
43
48
  */
44
49
  export function useBlazeSWR(cache, key, fetcher = null, options = {}) {
45
50
  const { use, ...swrOptions } = options;
46
51
  return useSWR(key, fetcher, {
47
- // `ttlMs`/`tags` deliberately stay *in* the config: SWR merges an `SWRConfig`
48
- // provider's values into it too, and hook-level options win that merge — so a
49
- // per-call stamp reaches the middleware's `config.ttlMs ?? options.ttlMs ?? default`
50
- // read as the highest precedence. Stripping them into the middleware's own options
51
- // would invert that: the provider's config value would then shadow the per-call one.
52
+ // `ttlMs`/`tags`/`forceRefresh` deliberately stay *in* the config: SWR merges an
53
+ // `SWRConfig` provider's values into it too, and hook-level options win that merge —
54
+ // so a per-call stamp reaches the middleware's `config.ttlMs ?? options.ttlMs ??
55
+ // default` read as the highest precedence. Stripping them into the middleware's own
56
+ // options would invert that: the provider's config value would then shadow the
57
+ // per-call one.
52
58
  ...swrOptions,
53
- use: [blazeSWRMiddleware(cache, { ttlMs: options.ttlMs, tags: options.tags }), ...(use ?? [])],
59
+ use: [
60
+ blazeSWRMiddleware(cache, {
61
+ ttlMs: options.ttlMs,
62
+ tags: options.tags,
63
+ forceRefresh: options.forceRefresh,
64
+ }),
65
+ ...(use ?? []),
66
+ ],
54
67
  });
55
68
  }
56
69
  //# sourceMappingURL=useBlazeSWR.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useBlazeSWR.js","sourceRoot":"","sources":["../src/useBlazeSWR.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,KAAK,CAAC;AAGzB,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAGrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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CAAC,YAAY;aACnC,CAAC;YACF,GAAG,CAAC,GAAG,IAAI,EAAE,CAAC;SACf;KACF,CAA6B,CAAC;AACjC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaze-cache/swr",
3
- "version": "0.3.0",
3
+ "version": "0.4.0-beta.0",
4
4
  "description": "Persist SWR in WebAssembly — instant first paint after reload while stale-while-revalidate still works",
5
5
  "keywords": [
6
6
  "swr",
@@ -35,7 +35,7 @@
35
35
  "dist"
36
36
  ],
37
37
  "dependencies": {
38
- "@blaze-cache/core": "0.3.0"
38
+ "@blaze-cache/core": "0.4.0-beta.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "swr": "^2.0.0"