Skip to content

Commit 196aa30

Browse files
committed
Fix stupid mistake
1 parent 4c58d04 commit 196aa30

File tree

3 files changed

+98
-60
lines changed

3 files changed

+98
-60
lines changed

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

+89-54
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/
1010
import { unstable_cache } from 'next/cache';
1111
import { getCloudflareContext } from './cloudflare';
1212
import { DataFetcherError, wrapDataFetcherError } from './errors';
13-
import { memoize } from './memoize';
13+
import { getCacheKey, memoize } from './memoize';
1414
import type { GitBookDataFetcher } from './types';
1515

1616
interface DataFetcherInput {
@@ -201,9 +201,10 @@ const getUserById = memoize(async function getUserById(
201201
});
202202
});
203203
},
204-
[],
204+
[input.apiToken ?? '', params.userId],
205205
{
206206
revalidate: 60 * 60 * 24,
207+
tags: [],
207208
}
208209
);
209210

@@ -240,14 +241,15 @@ const getSpace = memoize(async function getSpace(
240241
});
241242
});
242243
},
243-
[
244-
getCacheTag({
245-
tag: 'space',
246-
space: params.spaceId,
247-
}),
248-
],
244+
[input.apiToken ?? '', params.spaceId, params.shareKey ?? ''],
249245
{
250246
revalidate: 60 * 60 * 24,
247+
tags: [
248+
getCacheTag({
249+
tag: 'space',
250+
space: params.spaceId,
251+
}),
252+
],
251253
}
252254
);
253255

@@ -293,15 +295,16 @@ const getChangeRequest = memoize(async function getChangeRequest(
293295
});
294296
});
295297
},
296-
[
297-
getCacheTag({
298-
tag: 'change-request',
299-
space: params.spaceId,
300-
changeRequest: params.changeRequestId,
301-
}),
302-
],
298+
[input.apiToken ?? '', params.spaceId, params.changeRequestId ?? ''],
303299
{
304300
revalidate: 60 * 5,
301+
tags: [
302+
getCacheTag({
303+
tag: 'change-request',
304+
space: params.spaceId,
305+
changeRequest: params.changeRequestId,
306+
}),
307+
],
305308
}
306309
);
307310

@@ -353,9 +356,10 @@ const getRevision = memoize(async function getRevision(
353356
});
354357
});
355358
},
356-
[],
359+
[input.apiToken ?? '', params.spaceId, params.revisionId],
357360
{
358361
revalidate: 60 * 60 * 24 * 7,
362+
tags: [],
359363
}
360364
);
361365

@@ -399,9 +403,10 @@ const getRevisionPages = memoize(async function getRevisionPages(
399403
});
400404
});
401405
},
402-
[],
406+
[input.apiToken ?? '', params.spaceId, params.revisionId],
403407
{
404408
revalidate: 60 * 60 * 24,
409+
tags: [],
405410
}
406411
);
407412

@@ -448,9 +453,10 @@ const getRevisionFile = memoize(async function getRevisionFile(
448453
});
449454
});
450455
},
451-
[],
456+
[input.apiToken ?? '', params.spaceId, params.revisionId, params.fileId],
452457
{
453458
revalidate: 60 * 60 * 24 * 7,
459+
tags: [],
454460
}
455461
);
456462

@@ -500,9 +506,10 @@ const getRevisionPageMarkdown = memoize(async function getRevisionPageMarkdown(
500506
});
501507
});
502508
},
503-
[],
509+
[input.apiToken ?? '', params.spaceId, params.revisionId, params.pageId],
504510
{
505511
revalidate: 60 * 60 * 24 * 7,
512+
tags: [],
506513
}
507514
);
508515

@@ -555,9 +562,10 @@ const getRevisionPageByPath = memoize(async function getRevisionPageByPath(
555562
});
556563
});
557564
},
558-
[],
565+
[input.apiToken ?? '', params.spaceId, params.revisionId, params.path],
559566
{
560567
revalidate: 60 * 60 * 24 * 7,
568+
tags: [],
561569
}
562570
);
563571

@@ -603,9 +611,10 @@ const getDocument = memoize(async function getDocument(
603611
});
604612
});
605613
},
606-
[],
614+
[input.apiToken ?? '', params.spaceId, params.documentId],
607615
{
608616
revalidate: 60 * 60 * 24 * 7,
617+
tags: [],
609618
}
610619
);
611620

@@ -645,15 +654,22 @@ const getComputedDocument = memoize(async function getComputedDocument(
645654
});
646655
});
647656
},
648-
getComputedContentSourceCacheTags(
649-
{
650-
spaceId: params.spaceId,
651-
organizationId: params.organizationId,
652-
},
653-
params.source
654-
),
657+
[
658+
input.apiToken ?? '',
659+
params.spaceId,
660+
params.organizationId,
661+
getCacheKey([params.source]),
662+
params.seed,
663+
],
655664
{
656665
revalidate: 60 * 60 * 24,
666+
tags: getComputedContentSourceCacheTags(
667+
{
668+
spaceId: params.spaceId,
669+
organizationId: params.organizationId,
670+
},
671+
params.source
672+
),
657673
}
658674
);
659675

@@ -706,9 +722,10 @@ const getReusableContent = memoize(async function getReusableContent(
706722
});
707723
});
708724
},
709-
[],
725+
[input.apiToken ?? '', params.spaceId, params.revisionId, params.reusableContentId],
710726
{
711727
revalidate: 60 * 60 * 24 * 7,
728+
tags: [],
712729
}
713730
);
714731

@@ -749,15 +766,16 @@ const getLatestOpenAPISpecVersionContent = memoize(
749766
});
750767
});
751768
},
752-
[
753-
getCacheTag({
754-
tag: 'openapi',
755-
organization: params.organizationId,
756-
openAPISpec: params.slug,
757-
}),
758-
],
769+
[input.apiToken ?? '', params.organizationId, params.slug],
759770
{
760771
revalidate: 60 * 60 * 24,
772+
tags: [
773+
getCacheTag({
774+
tag: 'openapi',
775+
organization: params.organizationId,
776+
openAPISpec: params.slug,
777+
}),
778+
],
761779
}
762780
);
763781

@@ -810,14 +828,15 @@ const getPublishedContentSite = memoize(async function getPublishedContentSite(
810828
});
811829
});
812830
},
813-
[
814-
getCacheTag({
815-
tag: 'site',
816-
site: params.siteId,
817-
}),
818-
],
831+
[input.apiToken ?? '', params.organizationId, params.siteId, params.siteShareKey ?? ''],
819832
{
820833
revalidate: 60 * 60 * 24,
834+
tags: [
835+
getCacheTag({
836+
tag: 'site',
837+
site: params.siteId,
838+
}),
839+
],
821840
}
822841
);
823842

@@ -875,13 +894,20 @@ const getSiteRedirectBySource = memoize(async function getSiteRedirectBySource(
875894
});
876895
},
877896
[
878-
getCacheTag({
879-
tag: 'site',
880-
site: params.siteId,
881-
}),
897+
input.apiToken ?? '',
898+
params.organizationId,
899+
params.siteId,
900+
params.siteShareKey ?? '',
901+
params.source,
882902
],
883903
{
884904
revalidate: 60 * 60 * 24,
905+
tags: [
906+
getCacheTag({
907+
tag: 'site',
908+
site: params.siteId,
909+
}),
910+
],
885911
}
886912
);
887913

@@ -926,9 +952,10 @@ const getEmbedByUrl = memoize(async function getEmbedByUrl(
926952
});
927953
});
928954
},
929-
[],
955+
[input.apiToken ?? '', params.spaceId, params.url],
930956
{
931957
revalidate: 60 * 60 * 24 * 7,
958+
tags: [],
932959
}
933960
);
934961

@@ -964,9 +991,16 @@ const searchSiteContent = memoize(async function searchSiteContent(
964991
});
965992
});
966993
},
967-
[],
994+
[
995+
input.apiToken ?? '',
996+
params.organizationId,
997+
params.siteId,
998+
params.query,
999+
getCacheKey([params.scope]),
1000+
],
9681001
{
9691002
revalidate: 60 * 60 * 24,
1003+
tags: [],
9701004
}
9711005
);
9721006

@@ -1009,14 +1043,15 @@ const renderIntegrationUi = memoize(async function renderIntegrationUi(
10091043
});
10101044
});
10111045
},
1012-
[
1013-
getCacheTag({
1014-
tag: 'integration',
1015-
integration: params.integrationName,
1016-
}),
1017-
],
1046+
[input.apiToken ?? '', params.integrationName],
10181047
{
10191048
revalidate: 60 * 60 * 24,
1049+
tags: [
1050+
getCacheTag({
1051+
tag: 'integration',
1052+
integration: params.integrationName,
1053+
}),
1054+
],
10201055
}
10211056
);
10221057

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

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export function getCloudflareContext(): {
2020
*/
2121
ctx: Record<string, unknown>;
2222
} | null {
23+
if (process.env.NODE_ENV === 'development') {
24+
return null;
25+
}
26+
2327
try {
2428
// @ts-ignore
2529
return getCloudflareContextOpenNext();

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,14 @@ const perRequestCache = new WeakMap<object, WeakMap<any, CacheStorage<string, un
8181
*/
8282
async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<string, unknown>>> {
8383
try {
84-
const cloudflareContext = await getCloudflareContext();
84+
const cloudflareContext = getCloudflareContext();
8585
if (cloudflareContext?.cf) {
8686
// `cf` changes for each request, we can use it as an identifier of the request to isolate the cache per request
8787
const requestCache = perRequestCache.get(cloudflareContext.cf);
8888
if (requestCache) {
89-
console.log('Reusing per-request cache', cloudflareContext.cf);
9089
return requestCache;
9190
}
9291

93-
console.log('Allocating per-request cache', cloudflareContext.cf);
94-
9592
const newRequestCache = new WeakMap<any, CacheStorage<string, unknown>>();
9693
perRequestCache.set(cloudflareContext.cf, newRequestCache);
9794
return newRequestCache;
@@ -100,13 +97,15 @@ async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<strin
10097
if (process.env.NODE_ENV === 'production' && !process.env.VERCEL) {
10198
throw error;
10299
}
103-
104-
console.warn('Failed to get cloudflare context, using global cache', error);
105100
}
106101

107102
return globalCache;
108103
}
109104

105+
export function getCacheKey(args: any[]) {
106+
return JSON.stringify(deepSortValue(args));
107+
}
108+
110109
function deepSortValue(value: unknown): unknown {
111110
if (
112111
typeof value === 'string' ||

0 commit comments

Comments
 (0)