Server rendering
Server rendering comes in two kinds, and the adapter’s part differs between them.
Client components rendered on the server
Section titled “Client components rendered on the server”Server-side rendering turns your client components into HTML once, then hydrates them in the
browser. The adapter’s hooks support it through the server snapshot of useSyncExternalStore:
useQubee, anduseQubeeContextunder<QubeeProvider>with a configuration, get a fresh instance per tree. It renders its initial state on the server and hydrates to the same state — no mismatch.<QubeeProvider value={qubee}>hands the instance to you, so its lifetime is yours: create it per tree or per request, never as a module-level instance shared across server requests, and configure it the same way on both sides. A client-only write before hydration (fromlocalStorage, say) makes the client’s snapshot differ from the server HTML; make such writes in an effect.useBrowserAdapterknows no URL on the server, so a list that uses it renders its defaults there, and its real state right after hydrating. To render the right page on the server, use a router that knows the URL there — your framework’s.
React Server Components
Section titled “React Server Components”A Server Component never calls the hooks of @qubeejs/react: hooks and context exist only in
client components. For its own work it imports @qubeejs/core, which never imports React, and
fetchQubeePage from @qubeejs/react/fetch, an entry that is not a client module. Together they
read the URL, fetch the page and link onward:
import { buildListHref, buildListRequest, readListState } from '@qubeejs/core';import { fetchQubeePage } from '@qubeejs/react/fetch';
import { type Article, articleList } from '@/articles/article-list';
import { ArticleListView } from './article-list-view';
type ArticlesPageProps = { searchParams: Promise<Record<string, string | string[] | undefined>>;};
/** /articles, rendered on the server: read the URL, fetch the page, link onward. */export default async function ArticlesPage({ searchParams }: ArticlesPageProps) { const search = await searchParams; const state = readListState(articleList, search); const result = await fetchQubeePage<Article>(buildListRequest(articleList, state)); const nextHref = buildListHref(articleList, { pathname: '/articles', search }, { page: state.page + 1 });
return <ArticleListView nextHref={nextHref} result={result} />;}readListStatetakes Next’ssearchParamsrecord as it is.fetchQubeePagereturns a plain object — the only kind a Server Component can hand to a Client Component. Import it from@qubeejs/react/fetch: the main entry is a client module, and what a Server Component imports from a client module is a reference, not a function it can call.buildListHrefgives server-rendered links the same URL rules as the client: defaults left out, the page reset, foreign parameters kept.
The interactive half is a client component reading the same list with useQubeeList:
'use client';
import type { PaginatedResult } from '@qubeejs/core';import { useQubeeList } from '@qubeejs/react';import Link from 'next/link';
import { type Article, articleList } from '@/articles/article-list';
type ArticleListViewProps = { nextHref: string; result: PaginatedResult<Article>;};
/** * The interactive half of /articles: the search box, and the page the server fetched. The list * takes its adapter from the `<NextAdapter>` in the root layout. */export function ArticleListView({ nextHref, result }: ArticleListViewProps) { const list = useQubeeList(articleList);
return ( <section aria-busy={list.isPending}> <input aria-label="Search articles" onChange={(event) => list.set({ q: event.target.value }, { debounce: 300, replace: true })} type="search" value={list.state.q ?? ''} /> <ul> {result.data.map((article) => ( <li key={article.id}>{article.title}</li> ))} </ul> {list.state.page < (result.lastPage ?? 1) && <Link href={nextHref}>Next page</Link>} </section> );}When the search changes, the router navigates, Next renders the Server Component again with the new
searchParams, and the client component receives the new page as props. isPending stays true
until it has arrived. The list takes its adapter from the <NextAdapter> in the root layout — see
Next.js.
This page reads searchParams on the server, which makes the route dynamic: it renders on each
request, so the useSearchParams the adapter reads needs no <Suspense> boundary. A statically
rendered route that calls useSearchParams does need one above it, or next build fails. On such
a route nothing is fetched on the server: the client component fetches with
useQubeeQuery.
The Next.js entry
Section titled “The Next.js entry”@qubeejs/react/next ships the adapter used above: <NextAdapter> for the root layout, and
useNextAdapter() for the one list that should pass its own. It is an entry point of this
package, not a package of its own, with next as an optional peer dependency.
Next.js has the whole setup.