Skip to content

Commit 0096568

Browse files
author
Nicolas Dorseuil
committed
Always use React.cache()
1 parent 10026f3 commit 0096568

File tree

3 files changed

+19
-44
lines changed

3 files changed

+19
-44
lines changed

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-tags';
99
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
1010
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
11-
import { getCloudflareRequestGlobal } from './cloudflare';
1211
import { DataFetcherError, wrapDataFetcherError } from './errors';
1312
import { withCacheKey, withoutConcurrentExecution } from './memoize';
1413
import type { GitBookDataFetcher } from './types';
@@ -197,28 +196,24 @@ export function createDataFetcher(
197196
}
198197

199198
const getUserById = withCacheKey(
200-
withoutConcurrentExecution(
201-
getCloudflareRequestGlobal,
202-
async (_, input: DataFetcherInput, params: { userId: string }) => {
203-
'use cache';
204-
return trace(`getUserById(${params.userId})`, async () => {
205-
return wrapDataFetcherError(async () => {
206-
const api = apiClient(input);
207-
const res = await api.users.getUserById(params.userId, {
208-
...noCacheFetchOptions,
209-
});
210-
cacheTag(...getCacheTagsFromResponse(res));
211-
cacheLife('days');
212-
return res.data;
199+
withoutConcurrentExecution(async (_, input: DataFetcherInput, params: { userId: string }) => {
200+
'use cache';
201+
return trace(`getUserById(${params.userId})`, async () => {
202+
return wrapDataFetcherError(async () => {
203+
const api = apiClient(input);
204+
const res = await api.users.getUserById(params.userId, {
205+
...noCacheFetchOptions,
213206
});
207+
cacheTag(...getCacheTagsFromResponse(res));
208+
cacheLife('days');
209+
return res.data;
214210
});
215-
}
216-
)
211+
});
212+
})
217213
);
218214

219215
const getSpace = withCacheKey(
220216
withoutConcurrentExecution(
221-
getCloudflareRequestGlobal,
222217
async (
223218
_,
224219
input: DataFetcherInput,
@@ -255,7 +250,6 @@ const getSpace = withCacheKey(
255250

256251
const getChangeRequest = withCacheKey(
257252
withoutConcurrentExecution(
258-
getCloudflareRequestGlobal,
259253
async (
260254
_,
261255
input: DataFetcherInput,
@@ -294,7 +288,6 @@ const getChangeRequest = withCacheKey(
294288

295289
const getRevision = withCacheKey(
296290
withoutConcurrentExecution(
297-
getCloudflareRequestGlobal,
298291
async (
299292
_,
300293
input: DataFetcherInput,
@@ -325,7 +318,6 @@ const getRevision = withCacheKey(
325318

326319
const getRevisionPages = withCacheKey(
327320
withoutConcurrentExecution(
328-
getCloudflareRequestGlobal,
329321
async (
330322
_,
331323
input: DataFetcherInput,
@@ -356,7 +348,6 @@ const getRevisionPages = withCacheKey(
356348

357349
const getRevisionFile = withCacheKey(
358350
withoutConcurrentExecution(
359-
getCloudflareRequestGlobal,
360351
async (
361352
_,
362353
input: DataFetcherInput,
@@ -389,7 +380,6 @@ const getRevisionFile = withCacheKey(
389380

390381
const getRevisionPageMarkdown = withCacheKey(
391382
withoutConcurrentExecution(
392-
getCloudflareRequestGlobal,
393383
async (
394384
_,
395385
input: DataFetcherInput,
@@ -429,7 +419,6 @@ const getRevisionPageMarkdown = withCacheKey(
429419

430420
const getRevisionPageByPath = withCacheKey(
431421
withoutConcurrentExecution(
432-
getCloudflareRequestGlobal,
433422
async (
434423
_,
435424
input: DataFetcherInput,
@@ -463,7 +452,6 @@ const getRevisionPageByPath = withCacheKey(
463452

464453
const getDocument = withCacheKey(
465454
withoutConcurrentExecution(
466-
getCloudflareRequestGlobal,
467455
async (_, input: DataFetcherInput, params: { spaceId: string; documentId: string }) => {
468456
'use cache';
469457
return trace(`getDocument(${params.spaceId}, ${params.documentId})`, async () => {
@@ -488,7 +476,6 @@ const getDocument = withCacheKey(
488476

489477
const getComputedDocument = withCacheKey(
490478
withoutConcurrentExecution(
491-
getCloudflareRequestGlobal,
492479
async (
493480
_,
494481
input: DataFetcherInput,
@@ -538,7 +525,6 @@ const getComputedDocument = withCacheKey(
538525

539526
const getReusableContent = withCacheKey(
540527
withoutConcurrentExecution(
541-
getCloudflareRequestGlobal,
542528
async (
543529
_,
544530
input: DataFetcherInput,
@@ -571,7 +557,6 @@ const getReusableContent = withCacheKey(
571557

572558
const getLatestOpenAPISpecVersionContent = withCacheKey(
573559
withoutConcurrentExecution(
574-
getCloudflareRequestGlobal,
575560
async (_, input: DataFetcherInput, params: { organizationId: string; slug: string }) => {
576561
'use cache';
577562
cacheTag(
@@ -606,7 +591,6 @@ const getLatestOpenAPISpecVersionContent = withCacheKey(
606591

607592
const getPublishedContentSite = withCacheKey(
608593
withoutConcurrentExecution(
609-
getCloudflareRequestGlobal,
610594
async (
611595
_,
612596
input: DataFetcherInput,
@@ -647,7 +631,6 @@ const getPublishedContentSite = withCacheKey(
647631

648632
const getSiteRedirectBySource = withCacheKey(
649633
withoutConcurrentExecution(
650-
getCloudflareRequestGlobal,
651634
async (
652635
_,
653636
input: DataFetcherInput,
@@ -694,7 +677,6 @@ const getSiteRedirectBySource = withCacheKey(
694677

695678
const getEmbedByUrl = withCacheKey(
696679
withoutConcurrentExecution(
697-
getCloudflareRequestGlobal,
698680
async (_, input: DataFetcherInput, params: { spaceId: string; url: string }) => {
699681
'use cache';
700682
cacheTag(
@@ -727,7 +709,6 @@ const getEmbedByUrl = withCacheKey(
727709

728710
const searchSiteContent = withCacheKey(
729711
withoutConcurrentExecution(
730-
getCloudflareRequestGlobal,
731712
async (
732713
_,
733714
input: DataFetcherInput,
@@ -771,7 +752,6 @@ const searchSiteContent = withCacheKey(
771752

772753
const renderIntegrationUi = withCacheKey(
773754
withoutConcurrentExecution(
774-
getCloudflareRequestGlobal,
775755
async (
776756
_,
777757
input: DataFetcherInput,
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getCloudflareContext as getCloudflareContextOpenNext } from '@opennextjs/cloudflare';
2-
import { cache } from 'react';
32
import { GITBOOK_RUNTIME } from '../env';
43

54
/**
@@ -12,12 +11,3 @@ export function getCloudflareContext() {
1211

1312
return getCloudflareContextOpenNext();
1413
}
15-
16-
export function getCloudflareRequestGlobal() {
17-
const context = getCloudflareContext();
18-
if (!context) {
19-
return cache(() => ({}));
20-
}
21-
22-
return context.cf;
23-
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
import { cache } from 'react';
2+
3+
// This is used to create a context specific to the current request.
4+
// This version works both in cloudflare and in vercel.
5+
const getRequestContext = cache(() => ({}));
6+
17
/**
28
* Wrap a function by preventing concurrent executions of the same function.
39
* With a logic to work per-request in Cloudflare Workers.
410
*/
511
export function withoutConcurrentExecution<ArgsType extends any[], ReturnType>(
6-
getGlobalContext: () => object | null | undefined,
712
wrapped: (key: string, ...args: ArgsType) => Promise<ReturnType>
813
): (cacheKey: string, ...args: ArgsType) => Promise<ReturnType> {
914
const globalPromiseCache = new WeakMap<object, Map<string, Promise<ReturnType>>>();
1015

1116
return (key: string, ...args: ArgsType) => {
12-
const globalContext = getGlobalContext() ?? globalThis;
17+
const globalContext = getRequestContext();
1318

1419
/**
1520
* Cache storage that is scoped to the current request when executed in Cloudflare Workers,

0 commit comments

Comments
 (0)