Remote / foldkit-remote
remote
When server data lives in component hooks or an external store beside your application state, loading flags duplicate across screens, views reading overlapping fields drift out of sync, and optimistic updates fight the next network response.
Keep server-owned data in one normalized cache inside your application's Model
instead. Each screen declares only the fields it renders, active screens fetch
only what the cache lacks, and every server result returns as a Message through
update—so views stay pure, overlapping fields share one copy, and screens
replay from a recorded Model.
foldkit-remote is that normalized server cache for Foldkit applications. See
it running in the live Chirp demo.
Install
pnpm add foldkit-remote foldkit-entityfoldkit and effect are peer dependencies; foldkit-surface comes with the
package, and foldkit-remote-server runs the server side.
Example: render one server-owned project
Declare a Project entity, embed Remote.Model inside your application Model,
and render the project screen from a Surface:
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Entity, Remote, RemoteData } from 'foldkit-remote'
import { Surface } from 'foldkit-surface'
const Project = Entity.make('Project', Schema.Struct({ id: Schema.String, name: Schema.String }))
const ProjectSummary = Project.select({ id: true, name: true })
const Model = Schema.Struct({ remote: Remote.Model })
const Message = defineMessageUnion({ ...Remote.messages })
const App = Surface.application({ Model, Message })
const Data = Remote.make({ model: App.model.remote, entities: [Project] })
const ProjectPage = App.surface('ProjectPage', {
params: { projectId: Schema.String },
model: ({ params }) => ({ project: Data.get(ProjectSummary, params.projectId) }),
})
const viewProject = (model: typeof Model.Type, projectId: string) =>
RemoteData.render(ProjectPage.projection({ projectId }).read(model).project, {
loading: () => 'Loading project...',
notFound: () => 'Project not found.',
failed: error => `Could not load project: ${error.message}`,
data: (project, freshness) =>
freshness._tag === 'Fresh' ? project.name : `${project.name} (updating...)`,
})Depending on what model holds for projectId, viewProject returns:
'Loading project...'while the initial fetch is in flight (InitialorLoading).'Apollo'once the server row reduces intomodel(Ready).'Apollo (updating...)'while a refetch is in flight or after a failed refetch (Refreshingor staleFailed).'Project not found.'when the server confirms the row is absent (NotFound).'Could not load project: ...'when the request fails with no cached value (Failed).
What the example did:
Project.select({ id: true, name: true })narrows the screen's requirement to two fields.Data.get(ProjectSummary, params.projectId)declares whatProjectPageneeds from the Model so active subscriptions fetch missing fields.RemoteData.renderfolds all sixRemoteDatastates exhaustively instead of exposingundefinedor separate boolean flags.
When to use something else: foldkit-remote holds facts owned by the
server (plus a durable outbox for writes that must survive a reload). Use
foldkit-sync for client-authored edits that must merge across
offline devices.
Next:
- Wire
update, activesubscriptions, and an in-memory client: Full walkthrough: one entity, one Surface. - Compare with hook caches: If you know TanStack Query, TanStack DB or fate.
- Debug a read stuck at
Initial: Why is it stillInitial?.