1- import type { JSONDocument , RevisionPageDocument , Space } from '@gitbook/api' ;
1+ import { waitUntil } from '@/lib/waitUntil' ;
2+ import type { JSONDocument , RevisionPageDocument } from '@gitbook/api' ;
3+ import type { GitBookSiteContext , GitBookSpaceContext } from '../context' ;
24import { getDataOrNull } from './errors' ;
3- import type { GitBookDataFetcher } from './types' ;
45
56/**
67 * Get the document for a page.
78 */
89export async function getPageDocument (
9- dataFetcher : GitBookDataFetcher ,
10- space : Space ,
10+ context : GitBookSpaceContext | GitBookSiteContext ,
1111 page : RevisionPageDocument
1212) : Promise < JSONDocument | null > {
13+ const { dataFetcher, space } = context ;
14+
15+ if (
16+ 'site' in context &&
17+ ( context . site . id === 'site_JOVzv' || context . site . id === 'site_IxAYj' )
18+ ) {
19+ return getDataOrNull (
20+ dataFetcher . getRevisionPageDocument ( {
21+ spaceId : space . id ,
22+ revisionId : space . revision ,
23+ pageId : page . id ,
24+ } )
25+ ) ;
26+ }
27+
1328 if ( page . documentId ) {
1429 return getDataOrNull (
1530 dataFetcher . getDocument ( { spaceId : space . id , documentId : page . documentId } )
@@ -26,5 +41,30 @@ export async function getPageDocument(
2641 ) ;
2742 }
2843
44+ // Pre-fetch the document to start filling the cache before we migrate to this API.
45+ if ( isInPercentRollout ( space . id , 10 ) ) {
46+ await waitUntil (
47+ getDataOrNull (
48+ dataFetcher . getRevisionPageDocument ( {
49+ spaceId : space . id ,
50+ revisionId : space . revision ,
51+ pageId : page . id ,
52+ } )
53+ )
54+ ) ;
55+ }
56+
2957 return null ;
3058}
59+
60+ function isInPercentRollout ( value : string , rollout : number ) {
61+ return getRandomPercent ( value ) < rollout ;
62+ }
63+
64+ function getRandomPercent ( value : string ) {
65+ const hash = value . split ( '' ) . reduce ( ( acc , char ) => {
66+ return acc + char . charCodeAt ( 0 ) ;
67+ } , 0 ) ;
68+
69+ return hash % 100 ;
70+ }
0 commit comments