Skip to content
Setup/Next.js

Next.js

The adapter for the Next.js App Router is an entry point of its own, @qubeejs/react/next. It needs Next.js 15 or later.

Terminal window
npm install @qubeejs/react @qubeejs/core
app/layout.tsx
import { NextAdapter } from '@qubeejs/react/next';
import type { ReactNode } from 'react';
import { Providers } from './providers';
/**
* The root layout, a Server Component. It renders the adapter provider directly: the entry is a
* client module, and the provider takes only serialisable props.
*/
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
<NextAdapter>
<Providers>{children}</Providers>
</NextAdapter>
</body>
</html>
);
}

The root layout is a Server Component, and renders <NextAdapter> directly: the entry is a client module, and the provider takes only serialisable props. Every Client Component under it then navigates through the App Router.

The page is a Server Component. It reads the URL, fetches the page and builds links with @qubeejs/core and fetchQubeePage — neither imports React:

app/articles/page.tsx
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} />;
}

The interactive half is a Client Component that reads the same list with useQubeeList:

app/articles/article-list-view.tsx
'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.js renders the Server Component again with the new searchParams, and the Client Component receives the new page as a prop. isPending stays true until it has arrived. Server rendering explains each call.

  • A <Suspense> boundary on static routes. A component that uses a list on a statically rendered route needs a <Suspense> boundary above it, or next build fails: the adapter reads useSearchParams, which Next.js cannot resolve ahead of time. A route that reads searchParams on the server, like the one above, is dynamic and needs none. On a static route nothing is fetched on the server: the Client Component fetches with useQubeeQuery.
  • QubeeProvider and QubeeFetchProvider in a client file. One takes a driver and the other a function, which cannot cross the server–client boundary as props. Render them from a file of your own that starts with 'use client':
app/providers.tsx
'use client';
import { STRAPI_DRIVER } from '@qubeejs/core';
import { QubeeProvider } from '@qubeejs/react';
import type { ReactNode } from 'react';
/** The app's client-side providers, rendered by the root layout. */
export function Providers({ children }: { children: ReactNode }) {
return (
<QubeeProvider baseUrl={process.env.NEXT_PUBLIC_API_URL} driver={STRAPI_DRIVER}>
{children}
</QubeeProvider>
);
}
  • It reads usePathname() and useSearchParams(), and navigates with router.push and router.replace.
  • A list navigation does not scroll to the top; <NextAdapter scroll> — or useNextAdapter({ scroll: true }) — turns that scrolling back on.
  • A Server Component imports from @qubeejs/core, @qubeejs/react/fetch and @qubeejs/react/tanstack-query, which are not client modules. What it imports from @qubeejs/react it can render, not call.
  • A dialog or a picker inside a page keeps its list out of the URL with <MemoryAdapter>: the nearest provider wins.
  • The Pages Router is not supported.