Skip to content

Commit 1ded9c5

Browse files
committed
Avoid bursting cache
1 parent 5bade3e commit 1ded9c5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

packages/gitbook/src/lib/api.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ const getAPIContextId = async () => {
394394
* Get a revision by its ID.
395395
*/
396396
export const getRevision = cache({
397-
name: 'api.getRevision.v3',
397+
name: 'api.getRevision.v2',
398398
tag: (spaceId, revisionId, fetchOptions) =>
399399
// Temporary hack to make it work with OpenAPI on v1
400400
fetchOptions.tags?.[0] ??
@@ -427,19 +427,24 @@ export const getRevision = cache({
427427
return cacheResponse(response, {
428428
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
429429
data: {
430-
revision: response.data,
430+
...response.data,
431431
tags: getResponseCacheTags(response),
432432
},
433433
});
434434
},
435-
getKeyArgs: (args) => [args[0], args[1]],
435+
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
436+
if (fetchOptions.computed === false) {
437+
return [spaceId, revisionId, { computed: false }];
438+
}
439+
return [spaceId, revisionId];
440+
},
436441
});
437442

438443
/**
439444
* Get all the pages in a revision of a space.
440445
*/
441446
export const getRevisionPages = cache({
442-
name: 'api.getRevisionPages.v5',
447+
name: 'api.getRevisionPages.v4',
443448
tag: (spaceId, revisionId, fetchOptions) =>
444449
// Temporary hack to make it work with OpenAPI on v1
445450
fetchOptions.tags?.[0] ??
@@ -471,10 +476,15 @@ export const getRevisionPages = cache({
471476

472477
return cacheResponse(response, {
473478
...(fetchOptions.metadata ? cacheTtl_7days : cacheTtl_1day),
474-
data: { pages: response.data.pages, tags: getResponseCacheTags(response) },
479+
data: { ...response.data, tags: getResponseCacheTags(response) },
475480
});
476481
},
477-
getKeyArgs: (args) => [args[0], args[1]],
482+
getKeyArgs: ([spaceId, revisionId, fetchOptions]) => {
483+
if (fetchOptions.computed === false) {
484+
return [spaceId, revisionId, { computed: false }];
485+
}
486+
return [spaceId, revisionId];
487+
},
478488
});
479489

480490
/**
@@ -663,7 +673,7 @@ export const getRevisionFile = batch<[string, string, string], RevisionFile | nu
663673
let files: Record<string, RevisionFile> = {};
664674

665675
if (hasRevisionInMemory) {
666-
const { revision } = await getRevision(spaceId, revisionId, { metadata: false });
676+
const revision = await getRevision(spaceId, revisionId, { metadata: false });
667677
files = {};
668678
revision.files.forEach((file) => {
669679
files[file.id] = file;
@@ -709,7 +719,7 @@ export const getReusableContent = async (
709719
});
710720

711721
if (hasRevisionInMemory) {
712-
const { revision } = await getRevision(spaceId, revisionId, { metadata: false });
722+
const revision = await getRevision(spaceId, revisionId, { metadata: false });
713723
return (
714724
revision.reusableContents.find(
715725
(reusableContent) => reusableContent.id === reusableContentId

packages/gitbook/src/lib/v1.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
135135

136136
getRevision(params) {
137137
return wrapDataFetcherError(async () => {
138-
const { revision, tags } = await getRevision(params.spaceId, params.revisionId, {
138+
const { tags, ...revision } = await getRevision(params.spaceId, params.revisionId, {
139139
metadata: params.metadata,
140140
computed: false,
141141
});
@@ -145,11 +145,15 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
145145
(page) => page.type === RevisionPageType.Computed
146146
)
147147
) {
148-
const { revision } = await getRevision(params.spaceId, params.revisionId, {
149-
metadata: params.metadata,
150-
computed: true,
151-
tags,
152-
});
148+
const { tags: _tags, ...revision } = await getRevision(
149+
params.spaceId,
150+
params.revisionId,
151+
{
152+
metadata: params.metadata,
153+
computed: true,
154+
tags,
155+
}
156+
);
153157
return revision;
154158
}
155159

0 commit comments

Comments
 (0)