GitHub v1.3.1

server

Stable

The build-time execution block. Fetch data in Node.js, then hydrate the component state.

TypeScript Signature

typescript
Code copied!
  export async function server (context: {
    state: Record<string, any>;
    route: CoraliteRoute;
    page?: CoralitePage;
    store?: CoraliteStore;
    signal: AbortSignal;
    // plus any ubiquitous plugin context
  }): Promise<record<string, any> | void>
          </record<string,></string,>

Build-Time Only

The server() block runs in Node.js while the page is being generated. The compiler physically strips it from the module that ships to the browser, so credentials, database clients, and file paths never reach the client bundle.

The object you return is shallow-merged into state on top of the values produced by attributes. Anything returned here is available as a flat {{ token }} in the template and inside getters.

javascript
Code copied!
  export function server ({ page }) {
    return {
      title: page.title,
      posts: page.posts
    }
  }

Fetching Data

Because server() is asynchronous, you can await filesystem reads, database queries, or HTTP requests. Modules used only inside the block must be imported dynamically so they are never bundled for the browser.

javascript
Code copied!
  export async function server ({ state }) {
    const fs = await import('node:fs/promises')
    const raw = await fs.readFile('src/data/posts.json', 'utf8')
    const posts = JSON.parse(raw)
  
    return {
      posts: posts.slice(0, state.limit)
    }
  }

The Serialization Boundary

Everything returned from server() crosses a JSON boundary into the hydration script. Return plain, serializable data: strings, numbers, booleans, arrays, and plain objects. Class instances, functions, Date objects, and cyclic references cannot be serialized.

javascript
Code copied!
  // ✅ Plain data
  return { published: post.publishedAt.toISOString() }
  
  // ❌ Not serializable
  return { published: post.publishedAt }
CORALITE-E301: Static Build-Only Import

A top-level import inside server() is rejected. Load Node-only modules with await import('node:fs/promises') inside the block so the bundler can exclude them from the client output.

Deterministic Output

The build must be reproducible. Do not read wall-clock time, random values, or mutable global state directly in server(); feed that data in through the page context, a plugin, or the global store instead. The same inputs must always produce the same HTML.