Spoosh
Plugins

Initial Data

Provide initial data for queries

The initial data plugin lets you show data immediately before the fetch completes, such as prefetched or server-rendered data.

Installation

npm install @spoosh/plugin-initial-data

Usage

import { Spoosh } from "@spoosh/core";
import { initialDataPlugin } from "@spoosh/plugin-initial-data";

const client = new Spoosh<ApiSchema, Error>("/api").use([initialDataPlugin()]);

Show prefetched data immediately, then refetch in background:

const { data, meta } = useRead((api) => api("posts").GET(), {
  initialData: prefetchedPosts,
});

// Access via meta.isInitialData

Without Background Refetch

To skip the background refetch and just use the initial data:

const { data } = useRead((api) => api("posts").GET(), {
  initialData: prefetchedPosts,
  refetchOnInitialData: false,
});

Options

Per-Request Options

OptionTypeDefaultDescription
initialDataTData-Data to show immediately on first mount
refetchOnInitialDatabooleantrueWhether to refetch after showing initial data

Result

PropertyTypeDescription
meta.isInitialDatabooleantrue if currently showing initial data (not yet fetched)

Server-Side Rendering

createClient automatically generates Next.js cache tags from the API path:

import { createClient } from "@spoosh/core";

const api = createClient<ApiSchema>({ baseUrl: process.env.API_URL! });

// Auto-generates: next: { tags: ['posts'] }
const { data: posts } = await api("posts").GET();

For a complete SSR example with cache invalidation, see Next.js Plugin - With Initial Data.

On this page