The hooks in Reading Kookee content with React hooks fetch in an effect. Effects do not run on the server, so a hook on its own renders its loading state during SSR and only fetches once the page reaches the browser.
That is fixable in one line, and the same page can still paginate and refetch on the client afterwards.
Hand the loader's result to the hook
Fetch in your framework's loader and pass the result as initialData. The hook renders it immediately and skips its first request:
// TanStack Start
import { createFileRoute } from '@tanstack/react-router';
import { Kookee } from '@kookee/sdk';
import { useBlogPosts } from '@kookee/react';
// The loader also runs in the browser on client-side navigation, so read the public key from import.meta.env.
const kookee = new Kookee({ apiKey: import.meta.env.VITE_KOOKEE_API_KEY });
export const Route = createFileRoute('/blog')({
loader: () => kookee.blog.list({ limit: 10 }),
component: Blog,
});
function Blog() {
const loaderData = Route.useLoaderData();
const { data, hasMore, loadMore } = useBlogPosts({ limit: 10 }, { initialData: loaderData });
// Server-renders from loaderData. loadMore() still works on the client.
}
The key is public, so list your domains under Allowed Origins in Settings → Project; browsers on other sites are then refused. A projectId works in its place only while the project's hosted portal is enabled; without the portal the API answers 403 PORTAL_NOT_ENABLED.
The same shape works from a Next.js server component, which fetches and passes the result down as a prop:
// app/blog/page.tsx — server component
import { Kookee } from '@kookee/sdk';
import { BlogList } from './blog-list';
const kookee = new Kookee({ apiKey: process.env.KOOKEE_API_KEY });
export default async function Page() {
const firstPage = await kookee.blog.list({ limit: 10 });
return <BlogList initialData={firstPage} />;
}
// app/blog/blog-list.tsx
'use client';
import { useBlogPosts } from '@kookee/react';
import type { BlogEntryListItem, PaginatedResponse } from '@kookee/sdk';
export function BlogList({ initialData }: { initialData: PaginatedResponse<BlogEntryListItem> }) {
const { data, hasMore, loadMore } = useBlogPosts({ limit: 10 }, { initialData });
// ...
}
initialData is always the initialData key of the hook's last argument, and its type is whatever that hook's data is built from:
List hooks take the whole
PaginatedResponse<T>— the object withdata,total,pageandtotalPages, exactly whatlist()returns. Not a bare array.Single-entry hooks take the entry itself:
useBlogPost(slug, undefined, { initialData: post }).Array hooks take the array:
useHelpCategories(undefined, { initialData: categories }).
// A detail page, server-rendered.
const { data: post } = useBlogPost(slug, undefined, { initialData: loaderPost });
See Next.js integration guide for the routing side of this.
Or render on the server without a hook
A page that only displays content may not need a hook at all. The components in @kookee/react/blog, /changelog, /help, /roadmap and /entry take the data as props and render in a Server Component, so the list reaches the browser as HTML, with no fetch and no JavaScript of its own:
// app/blog/page.tsx — server component
import { BlogPostList } from '@kookee/react/blog';
export default async function Page() {
const { data } = await kookee.blog.list({ limit: 10 });
return <BlogPostList posts={data} href="/blog/{slug}" />;
}
Keep a hook with initialData for what has to change in the browser, such as a "Load more" button. See React components for your blog, changelog and help center.
The package root and @kookee/react/chat are marked 'use client', so a Server Component file can import KookeeProvider, the hooks and the chat widget directly. The hooks still run only in client components below it.
Scope initialData to the same parameters
initialData is bound to the parameters the hook is called with on its first render, not to the parameters your loader actually used. Nothing checks that the two agree.
So these must match:
// ✓ Loader and hook ask for the same thing.
loader: () => kookee.blog.list({ limit: 10, tags: ['release'] }),
const { data } = useBlogPosts({ limit: 10, tags: ['release'] }, { initialData: loaderData });
// ✗ The hook seeds 10 rows and then believes it is showing 25 of them.
loader: () => kookee.blog.list({ limit: 10 }),
const { data } = useBlogPosts({ limit: 25 }, { initialData: loaderData });
There is no error and no request in the second case — the hook simply serves the loader's page as though it answered the query it was given, and only fetches once something changes. Keep the parameters in one place if the two ever drift:
const blogParams = { limit: 10, tags: ['release'] } as const;
export const Route = createFileRoute('/blog')({
loader: () => kookee.blog.list(blogParams),
component: Blog,
});
function Blog() {
const { data } = useBlogPosts(blogParams, { initialData: Route.useLoaderData() });
}
Once the parameters change, the hook fetches normally. Change them back and the seeded page reappears without a request — which is what you want when a filter is applied and then cleared. After you call refetch(), the seed is retired for good: you asked for fresh data, so initialData is treated as stale from then on.
There is no caching
None at all. Two components calling useBlogPosts() make two requests. Nothing is shared between hooks, nothing is deduplicated, nothing refetches in the background, and nothing is kept when a component unmounts. That is deliberate — it is what keeps the package dependency-free and its behavior predictable.
In practice that leaves you three options.
Fetch once, high up. Call the hook in the page component and pass the result down as props, rather than calling the same hook in three cards.
Cache on the server. Do the fetching in your framework's loader, where its cache already lives, and hand the result over with initialData. This is usually the right answer for content that changes rarely. Caching strategies for Kookee content covers revalidation windows and CDN headers.
Bring your own query layer. useKookee() gives you the client; your library does the caching, deduplication and background refetching:
import { useQuery } from '@tanstack/react-query';
import { useKookee } from '@kookee/react';
function Blog() {
const kookee = useKookee();
const { data } = useQuery({
queryKey: ['blog', 'list', 10],
queryFn: ({ signal }) => kookee.blog.list({ limit: 10 }, signal),
});
}
Every SDK method takes an AbortSignal as its last argument, so it plugs into a query library's cancellation without a wrapper.
Requests cancel themselves
What the hooks do handle is request lifecycle. When a hook's inputs change or its component unmounts, the in-flight request is aborted, including a loadMore() page that was still arriving. Responses are also tagged, so one that lands out of order is discarded rather than written over newer data — a slow search for "re" cannot overwrite the results for "react".
An aborted request never surfaces as an error either. You will not see a spurious AbortError in error after navigating away, so no filtering is needed on your side:
// Typing quickly here fires and cancels several requests.
// Only the last one is ever rendered, and the cancelled ones stay silent.
const { data, error } = useHelpSearch(query);
The generic hooks
The named hooks are thin wrappers over three generics, which are exported for anything they do not cover — a new endpoint, a call with a shape the package has no name for, or your own composition:
import { useKookeeList, useKookeeQuery, useKookeeCollection, useKookee } from '@kookee/react';
useKookeeList— paginated responses, withloadMore,hasMore,totalandisLoadingMore.useKookeeQuery— a single value.dataisnulluntil it arrives.useKookeeCollection— methods that return a bare array.datais[]while loading.
Each takes a fetcher, a cache key and optional initialData. The list fetcher receives the page number and a signal; the other two receive only the signal:
function ReleaseNotes({ version }: { version: string }) {
const kookee = useKookee();
const { data, hasMore, loadMore } = useKookeeList(
(page, signal) => kookee.entries.list({ type: 'changelog', filter: { version }, limit: 20, page }, signal),
`release|${version}|20`,
);
}
const { data: translations } = useKookeeQuery(
(signal) => kookee.entries.getTranslationsBySlug(slug, signal),
`translations|${slug}`,
);
const { data: columns } = useKookeeCollection(
(signal) => kookee.feedback.getColumns(signal),
'feedback|columns',
);
Pass the signal through. It is what makes the cancellation above work.
The cache key rule
The second argument is the hook's only refetch trigger. The fetcher itself is deliberately left out of the effect's dependencies — it is a new function on every render, and depending on it would refetch forever — so:
The key must reflect everything the fetcher closes over.
// ✗ version is missing from the key. Switch versions and the hook never refetches:
// it keeps showing the first version's rows, with no error and no loading state.
useKookeeList(
(page, signal) => kookee.entries.list({ type: 'changelog', filter: { version }, page }, signal),
'release',
);
// ✓
useKookeeList(
(page, signal) => kookee.entries.list({ type: 'changelog', filter: { version }, page }, signal),
`release|${version}`,
);
Build the key however you like, as long as it is a string and it changes exactly when the request should. A template literal is enough for a couple of values; JSON.stringify handles an object, as long as the property order is stable:
const key = JSON.stringify(['release', { version, category, limit }]);
Note what a key is not. It is not a shared cache — two hooks with the same key share nothing, because nothing is cached anywhere. Its only job is to tell one hook instance when to fetch again. So the cost of a stale key is not a wrong cache hit; it is a request that never happens, which is much quieter and harder to spot. If a generic hook seems frozen on old data, the key is the first thing to check.
Keeping a generic hook idle
Pass null as the key and the hook does nothing: no request, isLoading false, data empty. This is how the named detail hooks stay idle on an unresolved route param, and it works the same way in your own:
function Comments({ articleId }: { articleId: string | undefined }) {
const kookee = useKookee();
const { data } = useKookeeList(
(page, signal) => kookee.entries.getComments(articleId as string, { page }, signal),
articleId ? `comments|${articleId}` : null,
);
}
The null key guards the call, so the cast inside the fetcher is safe — it never runs while the id is undefined.
Errors
error is typed unknown, because two quite different things can land there.
API failures are the SDK's KookeeApiError, carrying code and status:
import { KookeeApiError } from '@kookee/sdk';
function Article({ slug }: { slug: string | undefined }) {
const { data, isLoading, error } = useHelpArticle(slug);
if (isLoading) return <Spinner />;
if (error instanceof KookeeApiError && error.status === 404) return <NotFound />;
if (error) return <SomethingWentWrong />;
return <article>{data?.title}</article>;
}
Network and CORS failures never reach the API, so they reject as a plain TypeError from fetch — with no status and no code. Check the type before you read status, or a blocked origin turns into a crash inside your error branch:
// ✗ Throws on a CORS failure: error is a TypeError, and error.status is undefined.
if (error.status === 404) return <NotFound />;
// ✓
if (error instanceof KookeeApiError) {
if (error.status === 404) return <NotFound />;
if (error.status === 401 || error.status === 403) return <CheckYourKey />;
return <ApiProblem code={error.code} />;
}
if (error) return <OfflineOrBlocked />;
The distinction is diagnostic, not cosmetic. A TypeError here almost always means the request never left the browser: the origin is not in the project's allowed origins, the baseUrl is wrong, or the visitor is offline. A KookeeApiError means Kookee answered and said no. See Authentication and permission errors and Rate limits, plans and error handling for what the individual statuses mean.