Skip to content
Strata Sync

AI agents: fetch the documentation index at llms.txt. Markdown versions are available by appending .md to any page URL, including this page's markdown.

Server utilities

Server-side bootstrap prefetching and serialization for Next.js Server Components.

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

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

prefetchBootstrap

Fetches the full bootstrap dataset from the sync server.

// 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

OptionTypeDefaultDescription
endpointstringRequiredSync server endpoint URL.
authorizationstring-Authorization header value (such as "Bearer token").
headersRecord<string, string>-Additional request headers.
modelsstring[]-Specific models to fetch (all if omitted).
groupsstring[]-Sync groups to include.
schemaHashstring-Expected schema hash for validation.
timeoutnumber10000Request timeout in milliseconds.

BootstrapSnapshot

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.

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

OptionTypeDefaultDescription
compressbooleantrue (if CompressionStream available)Whether to gzip-compress the data.

BootstrapSnapshotPayload

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.

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.

// 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

OptionTypeDefaultDescription
endpointstringRequiredPrefetch API endpoint.
authorizationstring-Authorization header.
modelsArray<{ name, filter?, limit? }>RequiredModels to prefetch with optional filters.
timeoutnumber5000Request timeout in milliseconds.

PrefetchResult

Pass directly through the Server Component boundary as props:

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

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

Complete SSR example

See SSR and bootstrap for a complete Next.js App Router example.