Site / foldkit-site

Link clicks and URL changes

Site.routing wires link clicks and URL changes once for the whole tree so your update skips hand-writing both branches:

ts
import { modifyFields } from 'foldkit/struct'
import { ModelRef } from 'foldkit-surface'

const Routing = Site.routing<Model, Message, AppRoute>({
  site: AppSite,
  owner: App.owner,
  route: {
    dependency: ModelRef.focus(App.model.route).dependency,
    get: model => model.route,
    set: (model, route) => modifyFields(model, { route: () => route }),
  },
  parse: urlToAppRoute,
  tags: { clicked: 'ClickedLink', changed: 'ChangedUrl' },
  completed: Message.CompletedNavigation,
})

const assembly = Bundle.assemble<Model, Message>()([Routing, ...pages.map(page => page.placed)])
const update = assembly.update((model, message) =>
  Routing.reduces(message) ? { model } : updateOwn(model, message),
)
  • Link clicks: Links inside the tree navigate via push or replace according to Site.historyOf; external URLs and addresses outside AppSite (Site.routeAt) fall back to full document loads.
  • URL changes: Updates the route field and skips echoes when the parsed route matches the active one. Add arrived: (model, route) => ({ model, commands }) for post-navigation side effects like moving focus (examples/cms/src/apps/siteApp.ts).
  • History vs push: Provide navigated: (model, kind) => model to learn whether a route change came from 'history' (Back/Forward) or 'push' before setting the route — Remote uses this to keep lists intact on Back instead of refetching:
ts
const Navigating = Site.routing<Model, Message, AppRoute, RemoteClient>({
  // ...site, route, parse, tags, and completed as above
  navigated: (model, kind) => Data.reduce(model, { _tag: 'Navigated', kind }),
})

Routing.reduces(message) guards updateOwn from handling click or URL-change tags directly, while still letting foldkit-mirror URL mirrors read their slices first.