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

bash
pnpm add foldkit-remote foldkit-entity

foldkit 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:

ts
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 (Initial or Loading).
  • 'Apollo' once the server row reduces into model (Ready).
  • 'Apollo (updating...)' while a refetch is in flight or after a failed refetch (Refreshing or stale Failed).
  • '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 what ProjectPage needs from the Model so active subscriptions fetch missing fields.
  • RemoteData.render folds all six RemoteData states exhaustively instead of exposing undefined or 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: