Skip to content

Commit c64b422

Browse files
committed
Memoize one more
1 parent a27d740 commit c64b422

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

packages/gitbook-v2/src/lib/data/memoize.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import pMemoize from 'p-memoize';
22

3+
/**
4+
* We wrap 'use cache' calls in a p-memoize function to avoid
5+
* executing the function multiple times when doing concurrent calls.
6+
*
7+
* Hopefully one day this can be done directly by 'use cache'.
8+
*/
39
export function memoize<F extends (...args: any[]) => any>(f: F): F {
410
return pMemoize(f, {
511
cacheKey: (args) => {

packages/gitbook/src/lib/openapi/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
OpenAPISchemasBlock,
77
ResolveOpenAPIBlockArgs,
88
} from '@/lib/openapi/types';
9+
import { memoize } from '@v2/lib/data/memoize';
910
import { assert } from 'ts-essentials';
1011
import { resolveContentRef } from '../references';
1112
import { isV2 } from '../v2';
@@ -65,15 +66,15 @@ const fetchFilesystemV1 = cache({
6566
},
6667
});
6768

68-
async function fetchFilesystemV2(url: string) {
69+
const fetchFilesystemV2 = memoize(async function fetchFilesystemV2(url: string) {
6970
'use cache';
7071

7172
// TODO: add cache lifetime once we can use next.js 15 code here
7273

7374
const response = await fetchFilesystemUncached(url);
7475

7576
return response;
76-
}
77+
});
7778

7879
async function fetchFilesystemUncached(
7980
url: string,

0 commit comments

Comments
 (0)