Skip to content

Commit 5c95a33

Browse files
committed
Start new API
1 parent 3fbd6f0 commit 5c95a33

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"lockfileVersion": 1,
3+
"configVersion": 0,
34
"workspaces": {
45
"": {
56
"name": "gitbook",

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,44 @@ const getRevisionPageDocument = cache(
369369
}
370370
);
371371

372+
/**
373+
* Get the document for a page.
374+
* Compared to the v1 of `getRevisionPageDocument`, it dereferences the reusable content blocks.
375+
*/
376+
const getRevisionPageDocumentV2 = cache(
377+
async (
378+
input: DataFetcherInput,
379+
params: { spaceId: string; revisionId: string; pageId: string }
380+
) => {
381+
'use cache';
382+
return wrapCacheDataFetcherError(async () => {
383+
return trace(
384+
`getRevisionPageDocument(${params.spaceId}, ${params.revisionId}, ${params.pageId})`,
385+
async () => {
386+
const api = apiClient(input);
387+
const res = await api.spaces.getPageDocumentInRevisionById(
388+
params.spaceId,
389+
params.revisionId,
390+
params.pageId,
391+
{
392+
evaluated: 'deterministic-only',
393+
dereferenced: true,
394+
},
395+
{
396+
...noCacheFetchOptions,
397+
}
398+
);
399+
400+
cacheTag(...getCacheTagsFromResponse(res));
401+
cacheLifeFromResponse(res, 'max');
402+
403+
return res.data;
404+
}
405+
);
406+
});
407+
}
408+
);
409+
372410
const getRevisionReusableContentDocument = cache(
373411
async (
374412
input: DataFetcherInput,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* We often need to progressive rollout new data fetching methods to avoid hitting our API too hard.
3+
*/
4+
export function isRollout({
5+
discriminator,
6+
percentageRollout,
7+
}: {
8+
discriminator: string;
9+
percentageRollout: number;
10+
}): boolean {
11+
if (process.env.NODE_ENV === 'development') {
12+
return true;
13+
}
14+
15+
// compute a simple hash of the discriminator
16+
let hash = 0;
17+
for (let i = 0; i < discriminator.length; i++) {
18+
hash = (hash << 5) - hash + discriminator.charCodeAt(i);
19+
hash = hash & hash; // Convert to 32-bit integer
20+
}
21+
22+
return Math.abs(hash % 100) < percentageRollout;
23+
}

0 commit comments

Comments
 (0)