Site / foldkit-site

Setting a data policy per route

A route node does not carry a caching policy of its own; instead, pass a function of the Model to Data.wiring and exhaustively match model.route with your route union's match:

ts
const timeline = RemotePolicy.staleWhileRevalidate({ maxAge: 60_000 })
const profile = RemotePolicy.staleWhileRevalidate({ maxAge: 15_000 })

const wiring = Data.wiring(Site.sources(AppSite, App.owner, App.model.route), {
  policy: (model: Model) =>
    AppRoute.match(model.route, {
      Home: () => timeline,
      Profile: () => profile,
    }),
})

To override policy for one specific Surface (such as an always-fresh side editor), pass policies keyed by Surface name — TypeScript checks every key against the active record:

ts
const withEditor = Data.wiring(
  { ...Site.sources(AppSite, App.owner, App.model.route), editor },
  {
    policy: (model: Model) =>
      AppRoute.match(model.route, { Home: () => timeline, Profile: () => profile }),
    policies: { editor: RemotePolicy.networkOnly, Profile: RemotePolicy.cacheFirst },
  },
)

See examples/chirp/src/app.ts and examples/chirp/test/site.test.ts for this wiring tested per route.