Skip to content
Draft
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
1 change: 1 addition & 0 deletions bun.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"lockfileVersion": 1,
"configVersion": 0,
"workspaces": {
"": {
"name": "gitbook",
Expand Down
38 changes: 38 additions & 0 deletions packages/gitbook/src/lib/data/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions packages/gitbook/src/lib/rollout.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Loading