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:

posts = injectRead((api) => api("posts").GET(), {
  initialData: prefetchedPosts,
});

// Access via posts.meta().isInitialData

Without Background Refetch

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

posts = injectRead((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)

On this page