Skip to content

Commit a22bc52

Browse files
committed
improved docs for nesh cache and refactored try catch block
1 parent 79622eb commit a22bc52

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

packages/cache-handler/src/functions/nesh-cache.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,22 @@ type CommonNeshCacheOptions<Arguments extends unknown[], Result> = Omit<
170170
*
171171
* @param callback - The callback function to be cached.
172172
*
173-
* @param options - An object containing options for the cache.
173+
* @param commonOptions - An object containing options for the cache.
174174
*
175-
* @param options.tags - An array of tags to associate with the cached result.
175+
* @param commonOptions.tags - An array of tags to associate with the cached result.
176176
* Tags are used to revalidate the cache using the `revalidateTag` function.
177177
*
178-
* @param options.revalidate - The revalidation interval in seconds.
178+
* @param commonOptions.revalidate - The revalidation interval in seconds.
179179
* Must be a positive integer or `false` to disable revalidation.
180180
* Defaults to `export const revalidate = time;` in the current route.
181181
*
182-
* @param options.argumentsSerializer - A function that serializes the arguments passed to the callback function.
182+
* @param commonOptions.argumentsSerializer - A function that serializes the arguments passed to the callback function.
183183
* Use it to create a cache key. Defaults to `JSON.stringify(args)`.
184184
*
185-
* @param options.resultSerializer - A function that serializes the result of the callback function.
185+
* @param commonOptions.resultSerializer - A function that serializes the result of the callback function.
186186
* Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
187187
*
188-
* @param options.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
188+
* @param commonOptions.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
189189
* Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.
190190
*
191191
* @returns The callback wrapped in a caching function.
@@ -269,6 +269,8 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un
269269

270270
let cacheData: IncrementalCacheEntry | null = null;
271271

272+
let data: Result;
273+
272274
try {
273275
cacheData = await store.incrementalCache.get(key, {
274276
revalidate,
@@ -278,21 +280,13 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un
278280
fetchIdx,
279281
fetchUrl: 'neshCache',
280282
});
281-
} catch (error) {
282-
await handleUnlock();
283283

284-
throw error;
285-
}
284+
if (cacheData?.value?.kind === 'FETCH' && cacheData.isStale === false) {
285+
await handleUnlock();
286286

287-
if (cacheData?.value?.kind === 'FETCH' && cacheData.isStale === false) {
288-
await handleUnlock();
289-
290-
return resultDeserializer(cacheData.value.data.body);
291-
}
292-
293-
let data: Result;
287+
return resultDeserializer(cacheData.value.data.body);
288+
}
294289

295-
try {
296290
data = await staticGenerationAsyncStorage.run(
297291
{
298292
...store,
@@ -304,7 +298,6 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un
304298
...args,
305299
);
306300
} catch (error) {
307-
// biome-ignore lint/complexity/noUselessCatch: we need to rethrow the error
308301
throw error;
309302
} finally {
310303
await handleUnlock();

packages/cache-handler/src/functions/nesh-classic-cache.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,25 @@ type CommonNeshClassicCacheOptions<Arguments extends unknown[], Result> = Omit<
134134
*
135135
* @param callback - The callback function to be cached.
136136
*
137-
* @param options - An object containing options for the cache.
137+
* @param commonOptions - An object containing options for the cache.
138138
*
139-
* @param options.responseContext - The response context object.
139+
* @param commonOptions.responseContext - The response context object.
140140
* It is used to set the cache headers.
141141
*
142-
* @param options.tags - An array of tags to associate with the cached result.
142+
* @param commonOptions.tags - An array of tags to associate with the cached result.
143143
* Tags are used to revalidate the cache using the `revalidateTag` function.
144144
*
145-
* @param options.revalidate - The revalidation interval in seconds.
145+
* @param commonOptions.revalidate - The revalidation interval in seconds.
146146
* Must be a positive integer or `false` to disable revalidation.
147147
* Defaults to `export const revalidate = time;` in the current route.
148148
*
149-
* @param options.argumentsSerializer - A function that serializes the arguments passed to the callback function.
149+
* @param commonOptions.argumentsSerializer - A function that serializes the arguments passed to the callback function.
150150
* Use it to create a cache key. Defaults to `JSON.stringify(args)`.
151151
*
152-
* @param options.resultSerializer - A function that serializes the result of the callback function.
152+
* @param commonOptions.resultSerializer - A function that serializes the result of the callback function.
153153
* Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
154154
*
155-
* @param options.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
155+
* @param commonOptions.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
156156
* Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.
157157
*
158158
* @returns The callback wrapped in a caching function.

0 commit comments

Comments
 (0)