11import { type PagePathParams , fetchPageData } from '@/components/SitePage' ;
22import type { VisitorAuthClaims } from '@/lib/adaptive' ;
33import type { AncestorRevisionPage } from '@/lib/pages' ;
4- import { type ResolvedContentRef , resolveContentRef } from '@/lib/references' ;
4+ import {
5+ type ResolveContentRefOptions ,
6+ type ResolvedContentRef ,
7+ resolveContentRef ,
8+ } from '@/lib/references' ;
59import type { ContentRef , JSONDocument , RevisionPageDocument } from '@gitbook/api' ;
610import {
711 type RouteLayoutParams ,
@@ -10,7 +14,8 @@ import {
1014 getPagePathFromParams ,
1115 getStaticSiteContext ,
1216} from '@v2/app/utils' ;
13- import { cache } from 'react' ;
17+ import { identify } from 'object-identity' ;
18+ import { cache } from '../cache' ;
1419import type { GitBookSiteContext } from '../context' ;
1520import { getPageDocument } from './pages' ;
1621
@@ -39,7 +44,10 @@ export interface PrefetchedPageData {
3944 } ;
4045 } > ;
4146 document : Promise < JSONDocument | null > ;
42- prefetchedRef : Promise < Map < ContentRef , Promise < ResolvedContentRef | null > > > ;
47+ getPrefetchedRef : (
48+ ref ?: ContentRef ,
49+ options ?: ResolveContentRefOptions
50+ ) => Promise < ResolvedContentRef | null > ;
4351}
4452
4553const cachedInitialDate = cache ( ( ) => Date . now ( ) ) ;
@@ -107,14 +115,38 @@ export const prefetchedDocumentRef = (
107115 document : JSONDocument | null ,
108116 context : GitBookSiteContext
109117) => {
110- const fetched = new Map < ContentRef , Promise < ResolvedContentRef | null > > ( ) ;
118+ const fetched = new Map < string , Promise < ResolvedContentRef | null > > ( ) ;
111119 if ( ! document ) return fetched ;
112120
113121 const traverseNodes = ( nodes : any [ ] ) : void => {
114122 for ( const node of nodes ) {
115123 // We try prefetching as many references as possible.
116124 if ( node . data ?. ref ) {
117- fetched . set ( node . data . ref , resolveContentRef ( node . data . ref , context ) ) ;
125+ fetched . set ( identify ( node . data . ref ) , resolveContentRef ( node . data . ref , context ) ) ;
126+ }
127+ // Handle prefetching of references for cards
128+ if ( node . data ?. view && node . data . view . type === 'cards' ) {
129+ const view = node . data . view ;
130+ const records = Object . entries ( node . data . records || { } ) ;
131+
132+ records . forEach ( async ( record : [ string , any ] ) => {
133+ const coverFile = view . coverDefinition
134+ ? record [ 1 ] . values [ view . coverDefinition ] ?. [ 0 ]
135+ : null ;
136+ const targetRef = view . targetDefinition
137+ ? ( record [ 1 ] . values [ view . targetDefinition ] as ContentRef )
138+ : null ;
139+ if ( targetRef ) {
140+ fetched . set ( identify ( targetRef ) , resolveContentRef ( targetRef , context ) ) ;
141+ }
142+ if ( coverFile ) {
143+ const fileRef = {
144+ kind : 'file' as const ,
145+ file : coverFile ,
146+ } ;
147+ fetched . set ( identify ( fileRef ) , resolveContentRef ( fileRef , context ) ) ;
148+ }
149+ } ) ;
118150 }
119151 if ( node . nodes && Array . isArray ( node . nodes ) ) {
120152 traverseNodes ( node . nodes ) ;
@@ -165,9 +197,24 @@ export const getPrefetchedDataFromPageParams = cache((params: RouteParams): Pref
165197 . finally ( ( ) => {
166198 console . log ( `Finished prefetching references in ${ Date . now ( ) - startingDate } ms` ) ;
167199 } ) ;
200+
201+ const getContentRef = async (
202+ ref ?: ContentRef ,
203+ options ?: ResolveContentRefOptions
204+ ) : Promise < ResolvedContentRef | null > => {
205+ if ( ! ref ) {
206+ return null ;
207+ }
208+ if ( options ) {
209+ const { context } = await staticSiteContext ;
210+ return resolveContentRef ( ref , context , options ) ;
211+ }
212+ return prefetchedRef . then ( ( prefetched ) => prefetched . get ( identify ( ref ) ) ?? null ) ;
213+ } ;
214+
168215 return {
169216 pageData,
170217 document,
171- prefetchedRef ,
218+ getPrefetchedRef : getContentRef ,
172219 } ;
173220} ) ;
0 commit comments