Skip to content

Latest commit

 

History

History

basic

README.md

Farm.js Basic Example

This is a basic example demonstrating the core features of Farm.js without any complex dependencies or configurations.

Features Demonstrated

  • ✅ File-based routing
  • ✅ React Server Components
  • ✅ TypeScript support
  • ✅ Zero configuration setup
  • ✅ Vite-powered development
  • ✅ Layouts and nested routing

Running the Example

# From the root of the monorepo
pnpm install
pnpm build

# Start the example
cd examples/basic
pnpm dev

The example will be available at http://localhost:3000.

Required config for route boundaries

This example enables route-level loading.tsx and error.tsx boundaries with:

// examples/basic/farm.config.ts
export default defineConfig({
  serverComponents: true,
});

serverComponents: true is the important part for the current loading/error boundary behavior in this example.

The example also keeps deployment and Vite tweaks in the same file:

export default defineConfig({
  deploy: {
    target: "vercel",
  },
  vite: {
    // optional Vite customization
  },
});

No root vite.config.ts or vercel.json is required for the basic example.

Markdown page mirrors

This example exposes agent-readable markdown mirrors for a few normal pages:

export default defineConfig({
  md: {
    expose: ["/", "/about", "/contact"],
    cache: 60,
  },
});

With the dev server running, /about.md renders the same /about page as text/markdown.

PPR static shell

PPR is experimental and gated by experimental.ppr: true in farm.config.ts (this example enables it). With the flag on, the /ppr-demo page opts into static-shell caching:

export const experimental_ppr = true;
export const revalidate = 60;

The first request returns X-Farm-PPR: miss; the next matching request returns X-Farm-PPR: hit. Cached hits serve the Suspense fallback shell first, then refresh the dynamic island with a same-origin bypass request.

Cache and PPR observability

Farm can emit runtime events for cache, PPR, rendering, routes, APIs, integrations, middleware, storage, build, plugins, and errors. This example can enable the default logs or send events to a custom handler from farm.config.ts:

export default defineConfig({
  observability: {
    logs: true,
    onEvent(event) {
      if (event.type.startsWith("ppr.") || event.type.startsWith("cache.")) {
        console.log(event);
      }
    },
  },
});

Monorepo note

If you change code in packages/farm or packages/farmjs-plugin, rebuild the workspace packages before restarting this example:

pnpm --filter @farm.js/core build
pnpm --filter @farm.js/plugin build
cd examples/basic
pnpm dev

The example consumes the built package output from dist, so source changes in the framework packages are not picked up until they are rebuilt.

Project Structure

farm.config.ts
src/
  app/
    layout.tsx      # Root layout with navigation
    page.tsx        # Home page (/)
    error.tsx       # Route-level error boundary (catches errors for this segment)
    loading.tsx     # Route-level loading UI (Suspense fallback while segment loads)
    about/
      page.tsx      # About page (/about)
    contact/
      page.tsx      # Contact page (/contact)

Route-level boundaries (Next.js-style)

  • error.tsx – Catches runtime errors during SSR or client render for that route segment. Receives error and reset(). When an error is thrown (e.g. in a page or async component), this UI is shown instead; "Try again" calls reset() to re-render.
  • loading.tsx – Shown while the route segment is loading (e.g. async page/layout, streaming). Wraps the segment in a Suspense boundary; you get instant loading states during navigation and SSR streaming.

Important notes:

  • You do not manually import loading.tsx into page.tsx
  • The segment must suspend or throw for the boundary to appear
  • loading.tsx can stay a normal component; add 'use client' only if it truly needs client-only APIs
  • See src/app/boundaries/loading for the streamed loading example
  • See src/app/boundaries/suspense for a plain React Suspense example

What to Explore

  1. File-based Routing: Notice how each page.tsx file creates a new route
  2. Layouts: The root layout provides consistent navigation across all pages
  3. TypeScript: Full type safety with PageProps and other Farm.js types
  4. Zero Config: No complex configuration needed - just start coding!

Next Steps

After exploring this basic example, check out: