Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nesh cache fix #888

Open
wants to merge 4 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ next-env.d.ts

# act
.actrc
.act.json
.act.json

# jetbrains
.idea
2 changes: 1 addition & 1 deletion apps/cache-testing/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = {
parserOptions: {
project: true,
},
ignorePatterns: ['*.mjs'],
ignorePatterns: ['*.mjs', '*.js'],
};
2 changes: 1 addition & 1 deletion internal/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@typescript-eslint/parser": "7.18.0",
"@vercel/style-guide": "6.0.0",
"eslint-config-prettier": "9.1.0",
"eslint-config-turbo": "2.3.1",
"eslint-config-turbo": "2.3.2",
"eslint-plugin-only-warn": "1.1.0",
"typescript": "5.7.2"
}
Expand Down
83 changes: 41 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"@repo/prettier-config": "*",
"@repo/typescript-config": "*",
"glob": "11.0.0",
"prettier": "3.3.3",
"prettier": "3.4.1",
"rimraf": "6.0.1",
"turbo": "2.3.1"
"turbo": "2.3.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
31 changes: 12 additions & 19 deletions packages/cache-handler/src/functions/nesh-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,22 @@ type CommonNeshCacheOptions<Arguments extends unknown[], Result> = Omit<
*
* @param callback - The callback function to be cached.
*
* @param options - An object containing options for the cache.
* @param commonOptions - An object containing options for the cache.
*
* @param options.tags - An array of tags to associate with the cached result.
* @param commonOptions.tags - An array of tags to associate with the cached result.
* Tags are used to revalidate the cache using the `revalidateTag` function.
*
* @param options.revalidate - The revalidation interval in seconds.
* @param commonOptions.revalidate - The revalidation interval in seconds.
* Must be a positive integer or `false` to disable revalidation.
* Defaults to `export const revalidate = time;` in the current route.
*
* @param options.argumentsSerializer - A function that serializes the arguments passed to the callback function.
* @param commonOptions.argumentsSerializer - A function that serializes the arguments passed to the callback function.
* Use it to create a cache key. Defaults to `JSON.stringify(args)`.
*
* @param options.resultSerializer - A function that serializes the result of the callback function.
* @param commonOptions.resultSerializer - A function that serializes the result of the callback function.
* Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
*
* @param options.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
* @param commonOptions.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
* Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.
*
* @returns The callback wrapped in a caching function.
Expand Down Expand Up @@ -269,6 +269,8 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un

let cacheData: IncrementalCacheEntry | null = null;

let data: Result;

try {
cacheData = await store.incrementalCache.get(key, {
revalidate,
Expand All @@ -278,21 +280,13 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un
fetchIdx,
fetchUrl: 'neshCache',
});
} catch (error) {
await handleUnlock();

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

if (cacheData?.value?.kind === 'FETCH' && cacheData.isStale === false) {
await handleUnlock();

return resultDeserializer(cacheData.value.data.body);
}

let data: Result;
return resultDeserializer(cacheData.value.data.body);
}

try {
data = await staticGenerationAsyncStorage.run(
{
...store,
Expand All @@ -304,7 +298,6 @@ export function neshCache<Arguments extends unknown[], Result extends Promise<un
...args,
);
} catch (error) {
// biome-ignore lint/complexity/noUselessCatch: we need to rethrow the error
throw error;
} finally {
await handleUnlock();
Expand Down
14 changes: 7 additions & 7 deletions packages/cache-handler/src/functions/nesh-classic-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,25 @@ type CommonNeshClassicCacheOptions<Arguments extends unknown[], Result> = Omit<
*
* @param callback - The callback function to be cached.
*
* @param options - An object containing options for the cache.
* @param commonOptions - An object containing options for the cache.
*
* @param options.responseContext - The response context object.
* @param commonOptions.responseContext - The response context object.
* It is used to set the cache headers.
*
* @param options.tags - An array of tags to associate with the cached result.
* @param commonOptions.tags - An array of tags to associate with the cached result.
* Tags are used to revalidate the cache using the `revalidateTag` function.
*
* @param options.revalidate - The revalidation interval in seconds.
* @param commonOptions.revalidate - The revalidation interval in seconds.
* Must be a positive integer or `false` to disable revalidation.
* Defaults to `export const revalidate = time;` in the current route.
*
* @param options.argumentsSerializer - A function that serializes the arguments passed to the callback function.
* @param commonOptions.argumentsSerializer - A function that serializes the arguments passed to the callback function.
* Use it to create a cache key. Defaults to `JSON.stringify(args)`.
*
* @param options.resultSerializer - A function that serializes the result of the callback function.
* @param commonOptions.resultSerializer - A function that serializes the result of the callback function.
* Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
*
* @param options.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
* @param commonOptions.resultDeserializer - A function that deserializes the string representation of the result of the callback function.
* Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.
*
* @returns The callback wrapped in a caching function.
Expand Down