Site / foldkit-site

Preloading links on hover or focus

Pass preload to Site.routing with pointedLinks() from foldkit-primitives/dom to warm a destination route before the user clicks:

ts
import { pointedLinks } from 'foldkit-primitives/dom'

const Routing = Site.routing<Model, Message, AppRoute, RemoteClient>({
  // ...site, route, parse, tags, and completed as above
  preload: {
    links: pointedLinks(),
    intended: Message.PointedAtLink, // PointedAtLink: { url: Url }
    read: there => Data.preload(there, actives),
  },
})

pointedLinks() emits same-origin links when a pointer or keyboard focus rests on them for 50 ms (pointedLinks({ rest: '100 millis' }) adjusts the delay), or immediately on touchstart. Links with data-preload="none" are skipped. Site.routing ignores the current route and external addresses, passes read the target Model with route set, and Data.preload writes fetched rows into Remote without updating model.route.

To also preload links currently visible in the viewport, merge visibleLinks({ atMost: 8 }) (skipped automatically under Save-Data, 2g, or hidden tabs):

ts
links: Stream.merge(pointedLinks(), visibleLinks({ atMost: 8 })),