Skip to content

Commit 711cf38

Browse files
authored
Use getRevision in getRevisionFile (#3379)
1 parent 4f7c0ee commit 711cf38

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

.changeset/lazy-colts-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-v2": patch
3+
---
4+
5+
Optimize the fetch of revision files by using only the getRevision cache.

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-t
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';
1111
import { cache } from '../cache';
12-
import { DataFetcherError, wrapCacheDataFetcherError } from './errors';
12+
import { DataFetcherError, throwIfDataError, wrapCacheDataFetcherError } from './errors';
1313
import type { GitBookDataFetcher } from './types';
1414

1515
interface DataFetcherInput {
@@ -73,7 +73,6 @@ export function createDataFetcher(
7373
getRevision(input, {
7474
spaceId: params.spaceId,
7575
revisionId: params.revisionId,
76-
metadata: params.metadata,
7776
})
7877
);
7978
},
@@ -282,10 +281,7 @@ const getChangeRequest = cache(
282281
);
283282

284283
const getRevision = cache(
285-
async (
286-
input: DataFetcherInput,
287-
params: { spaceId: string; revisionId: string; metadata: boolean }
288-
) => {
284+
async (input: DataFetcherInput, params: { spaceId: string; revisionId: string }) => {
289285
'use cache';
290286
return wrapCacheDataFetcherError(async () => {
291287
return trace(`getRevision(${params.spaceId}, ${params.revisionId})`, async () => {
@@ -294,7 +290,7 @@ const getRevision = cache(
294290
params.spaceId,
295291
params.revisionId,
296292
{
297-
metadata: params.metadata,
293+
metadata: true,
298294
},
299295
{
300296
...noCacheFetchOptions,
@@ -340,24 +336,24 @@ const getRevisionFile = cache(
340336
input: DataFetcherInput,
341337
params: { spaceId: string; revisionId: string; fileId: string }
342338
) => {
343-
'use cache';
344339
return wrapCacheDataFetcherError(async () => {
345340
return trace(
346341
`getRevisionFile(${params.spaceId}, ${params.revisionId}, ${params.fileId})`,
347342
async () => {
348-
const api = apiClient(input);
349-
const res = await api.spaces.getFileInRevisionById(
350-
params.spaceId,
351-
params.revisionId,
352-
params.fileId,
353-
{},
354-
{
355-
...noCacheFetchOptions,
356-
}
343+
const revision = await throwIfDataError(
344+
getRevision(input, {
345+
spaceId: params.spaceId,
346+
revisionId: params.revisionId,
347+
})
357348
);
358-
cacheTag(...getCacheTagsFromResponse(res));
359-
cacheLife('max');
360-
return res.data;
349+
350+
const file = revision.files.find((file) => file.id === params.fileId);
351+
352+
if (!file) {
353+
throw new DataFetcherError('File not found', 404);
354+
}
355+
356+
return file;
361357
}
362358
);
363359
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export interface GitBookDataFetcher {
6767
getRevision(params: {
6868
spaceId: string;
6969
revisionId: string;
70-
metadata: boolean;
7170
}): Promise<DataFetcherResponse<api.Revision>>;
7271

7372
/**

packages/gitbook/src/components/AdminToolbar/AdminToolbar.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ async function RevisionToolbar(props: { context: GitBookSiteContext }) {
112112
context.dataFetcher.getRevision({
113113
spaceId: space.id,
114114
revisionId,
115-
metadata: true,
116115
})
117116
);
118117

packages/gitbook/src/lib/v1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function getDataFetcherV1(apiTokenOverride?: string): GitBookDataFetcher {
179179
return withAPI(() =>
180180
wrapDataFetcherError(async () => {
181181
return getRevision(params.spaceId, params.revisionId, {
182-
metadata: params.metadata,
182+
metadata: true,
183183
});
184184
})
185185
);

0 commit comments

Comments
 (0)