Skip to content

Use getRevision in getRevisionFile #3379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-colts-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---

Optimize the fetch of revision files by using only the getRevision cache.
36 changes: 16 additions & 20 deletions packages/gitbook-v2/src/lib/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -73,7 +73,6 @@ export function createDataFetcher(
getRevision(input, {
spaceId: params.spaceId,
revisionId: params.revisionId,
metadata: params.metadata,
})
);
},
Expand Down Expand Up @@ -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 () => {
Expand All @@ -294,7 +290,7 @@ const getRevision = cache(
params.spaceId,
params.revisionId,
{
metadata: params.metadata,
metadata: true,
},
{
...noCacheFetchOptions,
Expand Down Expand Up @@ -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;
}
);
});
Expand Down
1 change: 0 additions & 1 deletion packages/gitbook-v2/src/lib/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export interface GitBookDataFetcher {
getRevision(params: {
spaceId: string;
revisionId: string;
metadata: boolean;
}): Promise<DataFetcherResponse<api.Revision>>;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ async function RevisionToolbar(props: { context: GitBookSiteContext }) {
context.dataFetcher.getRevision({
spaceId: space.id,
revisionId,
metadata: true,
})
);

Expand Down
2 changes: 1 addition & 1 deletion packages/gitbook/src/lib/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function getDataFetcherV1(apiTokenOverride?: string): GitBookDataFetcher {
return withAPI(() =>
wrapDataFetcherError(async () => {
return getRevision(params.spaceId, params.revisionId, {
metadata: params.metadata,
metadata: true,
});
})
);
Expand Down