Site / foldkit-site

site

In a multi-screen app, route strings, page titles, navigation sections, breadcrumbs, history rules, and data triggers end up duplicated across parallel lookup tables. Adding or renaming a route means updating four files by hand, and a mistyped link target silently sends users to a 404.

foldkit-site lets you declare your route topology once as a tree of typed nodes built on Foldkit's Router. Each node pairs a URL parser and route union variant with its title, section, history rule, layout, and active Surface. One tree parses URLs, constructs typed link targets (Site.target), drives navigation and breadcrumbs, and selects which data to fetch on the client and server.

See it running in examples/foldkit/routing and read Site, Remote and SSR together for a full server-rendered example.

Install

sh
pnpm add foldkit-site

Peers on effect and foldkit. Uses foldkit-surface for Site.sources and foldkit-ssr for Meta types.

A first route tree

This builds a three-route tree (Home, People, and Person), creates a typed link target for person 3, and reads its URL, route chain, title, and navigation section:

ts
import { Schema, pipe } from 'effect'
import { defineRouteUnion, int, literal, mapTo, root, slash } from 'foldkit/route'
import { Site } from 'foldkit-site'

const AppRoute = defineRouteUnion({
  Home: {},
  People: {},
  Person: { personId: Schema.Number },
})

const Home = Site.route(pipe(root, mapTo(AppRoute.Home)), AppRoute.Home, { title: () => 'Home' })
const People = Site.route(pipe(literal('people'), mapTo(AppRoute.People)), AppRoute.People)
const Person = Site.route(
  pipe(literal('people'), slash(int('personId')), mapTo(AppRoute.Person)),
  AppRoute.Person,
  { title: ({ personId }) => `Person ${personId}`, section: 'People' },
)

const AppSite = Site.make(Home, Site.mount(People, [Person]))
const target = Site.target(Person, { personId: 3 })

Evaluating target and the tree helpers produces:

  • target.url -> '/people/3'
  • target.route -> AppRoute.Person({ personId: 3 })
  • Site.chainOf(AppSite, target.route) -> [People, Person]
  • Site.titleOf(AppSite, target.route) -> 'Person 3'
  • Site.sectionOf(AppSite, target.route) -> 'People'

Site.route wraps your existing Foldkit Router (root, literal, slash, query, mapTo) and attaches structural metadata to it without re-parsing URLs.