Use getRevision in getRevisionFile (#3379)

This commit is contained in:
Samy Pessé
2025-06-23 18:43:41 +02:00
committed by GitHub
parent 4f7c0eea9f
commit 711cf38f9b
5 changed files with 22 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---
Optimize the fetch of revision files by using only the getRevision cache.
+16 -20
View File
@@ -9,7 +9,7 @@ import { getCacheTag, getComputedContentSourceCacheTags } from '@gitbook/cache-t
import { GITBOOK_API_TOKEN, GITBOOK_API_URL, GITBOOK_USER_AGENT } from '@v2/lib/env';
import { unstable_cacheLife as cacheLife, unstable_cacheTag as cacheTag } from 'next/cache';
import { cache } from '../cache';
import { DataFetcherError, wrapCacheDataFetcherError } from './errors';
import { DataFetcherError, throwIfDataError, wrapCacheDataFetcherError } from './errors';
import type { GitBookDataFetcher } from './types';
interface DataFetcherInput {
@@ -73,7 +73,6 @@ export function createDataFetcher(
getRevision(input, {
spaceId: params.spaceId,
revisionId: params.revisionId,
metadata: params.metadata,
})
);
},
@@ -282,10 +281,7 @@ const getChangeRequest = cache(
);
const getRevision = cache(
async (
input: DataFetcherInput,
params: { spaceId: string; revisionId: string; metadata: boolean }
) => {
async (input: DataFetcherInput, params: { spaceId: string; revisionId: string }) => {
'use cache';
return wrapCacheDataFetcherError(async () => {
return trace(`getRevision(${params.spaceId}, ${params.revisionId})`, async () => {
@@ -294,7 +290,7 @@ const getRevision = cache(
params.spaceId,
params.revisionId,
{
metadata: params.metadata,
metadata: true,
},
{
...noCacheFetchOptions,
@@ -340,24 +336,24 @@ const getRevisionFile = cache(
input: DataFetcherInput,
params: { spaceId: string; revisionId: string; fileId: string }
) => {
'use cache';
return wrapCacheDataFetcherError(async () => {
return trace(
`getRevisionFile(${params.spaceId}, ${params.revisionId}, ${params.fileId})`,
async () => {
const api = apiClient(input);
const res = await api.spaces.getFileInRevisionById(
params.spaceId,
params.revisionId,
params.fileId,
{},
{
...noCacheFetchOptions,
}
const revision = await throwIfDataError(
getRevision(input, {
spaceId: params.spaceId,
revisionId: params.revisionId,
})
);
cacheTag(...getCacheTagsFromResponse(res));
cacheLife('max');
return res.data;
const file = revision.files.find((file) => file.id === params.fileId);
if (!file) {
throw new DataFetcherError('File not found', 404);
}
return file;
}
);
});
@@ -67,7 +67,6 @@ export interface GitBookDataFetcher {
getRevision(params: {
spaceId: string;
revisionId: string;
metadata: boolean;
}): Promise<DataFetcherResponse<api.Revision>>;
/**
@@ -112,7 +112,6 @@ async function RevisionToolbar(props: { context: GitBookSiteContext }) {
context.dataFetcher.getRevision({
spaceId: space.id,
revisionId,
metadata: true,
})
);
+1 -1
View File
@@ -179,7 +179,7 @@ function getDataFetcherV1(apiTokenOverride?: string): GitBookDataFetcher {
return withAPI(() =>
wrapDataFetcherError(async () => {
return getRevision(params.spaceId, params.revisionId, {
metadata: params.metadata,
metadata: true,
});
})
);