diff --git a/bun.lock b/bun.lock index 5b9d520f03..5612cc57e3 100644 --- a/bun.lock +++ b/bun.lock @@ -1,5 +1,6 @@ { "lockfileVersion": 1, + "configVersion": 0, "workspaces": { "": { "name": "gitbook", diff --git a/packages/gitbook/src/lib/data/api.ts b/packages/gitbook/src/lib/data/api.ts index b66af936ce..5fc4a69d2d 100644 --- a/packages/gitbook/src/lib/data/api.ts +++ b/packages/gitbook/src/lib/data/api.ts @@ -369,6 +369,44 @@ const getRevisionPageDocument = cache( } ); +/** + * Get the document for a page. + * Compared to the v1 of `getRevisionPageDocument`, it dereferences the reusable content blocks. + */ +const getRevisionPageDocumentV2 = cache( + async ( + input: DataFetcherInput, + params: { spaceId: string; revisionId: string; pageId: string } + ) => { + 'use cache'; + return wrapCacheDataFetcherError(async () => { + return trace( + `getRevisionPageDocument(${params.spaceId}, ${params.revisionId}, ${params.pageId})`, + async () => { + const api = apiClient(input); + const res = await api.spaces.getPageDocumentInRevisionById( + params.spaceId, + params.revisionId, + params.pageId, + { + evaluated: 'deterministic-only', + dereferenced: true, + }, + { + ...noCacheFetchOptions, + } + ); + + cacheTag(...getCacheTagsFromResponse(res)); + cacheLifeFromResponse(res, 'max'); + + return res.data; + } + ); + }); + } +); + const getRevisionReusableContentDocument = cache( async ( input: DataFetcherInput, diff --git a/packages/gitbook/src/lib/rollout.ts b/packages/gitbook/src/lib/rollout.ts new file mode 100644 index 0000000000..43b7360fa7 --- /dev/null +++ b/packages/gitbook/src/lib/rollout.ts @@ -0,0 +1,23 @@ +/** + * We often need to progressive rollout new data fetching methods to avoid hitting our API too hard. + */ +export function isRollout({ + discriminator, + percentageRollout, +}: { + discriminator: string; + percentageRollout: number; +}): boolean { + if (process.env.NODE_ENV === 'development') { + return true; + } + + // compute a simple hash of the discriminator + let hash = 0; + for (let i = 0; i < discriminator.length; i++) { + hash = (hash << 5) - hash + discriminator.charCodeAt(i); + hash = hash & hash; // Convert to 32-bit integer + } + + return Math.abs(hash % 100) < percentageRollout; +}