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-dataUsage
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.isInitialDataWithout 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
| Option | Type | Default | Description |
|---|---|---|---|
initialData | TData | - | Data to show immediately on first mount |
refetchOnInitialData | boolean | true | Whether to refetch after showing initial data |
Result
| Property | Type | Description |
|---|---|---|
meta.isInitialData | boolean | true 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.