> ## Documentation Index
> Fetch the complete documentation index at: https://stratasync.blode.md/llms.txt
> Use this file to discover all available pages before exploring further.

# Server utilities

Prefetch sync data on the server and pass it to client components, eliminating the client-side bootstrap request.

```ts
import {
  prefetchBootstrap,
  prefetchModels,
  serializeBootstrapSnapshot,
  encodeBootstrapSnapshot,
} from "@stratasync/next/server";
```

## prefetchBootstrap

Fetches the full bootstrap dataset from the sync server.

```tsx
// app/dashboard/page.tsx (Server Component)
import {
  prefetchBootstrap,
  serializeBootstrapSnapshot,
} from "@stratasync/next/server";

export default async function DashboardPage() {
  const snapshot = await prefetchBootstrap({
    endpoint: process.env.SYNC_ENDPOINT!,
    authorization: `Bearer ${await getServerToken()}`,
    groups: ["workspace-abc"],
    timeout: 10000,
  });

  const serialized = await serializeBootstrapSnapshot(snapshot);

  return <DashboardClient bootstrapData={serialized} />;
}
```

**Signature:** `prefetchBootstrap(options: PrefetchBootstrapOptions): Promise<BootstrapSnapshot>`

### PrefetchBootstrapOptions

| Option          | Type                     | Default  | Description                                            |
| --------------- | ------------------------ | -------- | ------------------------------------------------------ |
| `endpoint`      | `string`                 | Required | Sync server endpoint URL.                              |
| `authorization` | `string`                 | -        | Authorization header value (such as `"Bearer token"`). |
| `headers`       | `Record<string, string>` | -        | Additional request headers.                            |
| `models`        | `string[]`               | -        | Specific models to fetch (all if omitted).             |
| `groups`        | `string[]`               | -        | Sync groups to include.                                |
| `schemaHash`    | `string`                 | -        | Expected schema hash for validation.                   |
| `timeout`       | `number`                 | `10000`  | Request timeout in milliseconds.                       |

### BootstrapSnapshot

```ts
interface BootstrapSnapshot {
  version: 1;
  schemaHash: string;
  lastSyncId: number;
  firstSyncId?: number;
  groups: string[];
  rows: ModelRow[];
  fetchedAt: number;
  rowCount?: number;
}
```

## serializeBootstrapSnapshot

Serializes a snapshot for transfer across the Server Component boundary. Supports gzip compression.

```ts
const payload = await serializeBootstrapSnapshot(snapshot);
// payload.version: 1
// payload.encoding: "json" | "gzip-base64"
// payload.data: string
```

**Signature:** `serializeBootstrapSnapshot(snapshot: BootstrapSnapshot, options?: SerializeBootstrapOptions): Promise<BootstrapSnapshotPayload>`

### SerializeBootstrapOptions

| Option     | Type      | Default                                   | Description                        |
| ---------- | --------- | ----------------------------------------- | ---------------------------------- |
| `compress` | `boolean` | `true` (if `CompressionStream` available) | Whether to gzip-compress the data. |

### BootstrapSnapshotPayload

```ts
interface BootstrapSnapshotPayload {
  version: 1;
  encoding: "json" | "gzip-base64";
  data: string;
}
```

When compression is available (Node.js 18+), data is gzip-compressed and base64-encoded to reduce prop size.

## encodeBootstrapSnapshot

Wraps `serializeBootstrapSnapshot` + `JSON.stringify` into a single JSON string.

```ts
const encoded = await encodeBootstrapSnapshot(snapshot);
// encoded is a JSON string containing the BootstrapSnapshotPayload
```

**Signature:** `encodeBootstrapSnapshot(snapshot: BootstrapSnapshot, options?: SerializeBootstrapOptions): Promise<string>`

## prefetchModels

Prefetch a subset of models for a specific page.

```tsx
// app/dashboard/page.tsx (Server Component)
import { prefetchModels } from "@stratasync/next/server";

export default async function DashboardPage() {
  const result = await prefetchModels({
    endpoint: process.env.API_URL + "/sync/prefetch",
    authorization: `Bearer ${await getServerToken()}`,
    models: [
      { name: "Project", limit: 10 },
      { name: "Task", filter: { status: "active" }, limit: 50 },
    ],
    timeout: 5000,
  });

  return <DashboardClient prefetchedData={result} />;
}
```

**Signature:** `prefetchModels(options: PrefetchOptions): Promise<PrefetchResult>`

### PrefetchOptions

| Option          | Type                               | Default  | Description                               |
| --------------- | ---------------------------------- | -------- | ----------------------------------------- |
| `endpoint`      | `string`                           | Required | Prefetch API endpoint.                    |
| `authorization` | `string`                           | -        | Authorization header.                     |
| `models`        | `Array<{ name, filter?, limit? }>` | Required | Models to prefetch with optional filters. |
| `timeout`       | `number`                           | `5000`   | Request timeout in milliseconds.          |

### PrefetchResult

Pass directly through the Server Component boundary as props:

```ts
interface PrefetchResult {
  data: Record<string, PrefetchedData>;
}

interface PrefetchedData {
  modelName: string;
  data: Record<string, unknown>[];
  fetchedAt: number;
}
```

## Complete SSR example

See [SSR and bootstrap](/guides/ssr-bootstrap) for a complete Next.js App Router example.