Reference / @getenginex/sdk-next/server / prefetchEnginex
prefetchEnginex()
Call Signature
prefetchEnginex(
source,select):Promise<DehydratedState>
Prefetch of SDK queries in a Server Component, to hydrate the browser QueryClient with TanStack Query's <HydrationBoundary state={...}>, with the same keys the factories use on the client.
source: the server client (await getEnginexServer();selectreceives the core factories) or the factories of a product package (createIgamingQueries(igaming);selectreceives them as they came).- One
QueryClientper call: created here and discarded at the end; nothing stays between calls or requests. No retry on the server. - Failures: a factory that fails (gateway error, user query without a session) does not take down the others and stays out of the state; only successful queries are dehydrated (never an error). The browser fetches those normally.
- Session: with an anonymous client (the
getEnginexServerdefault), user queries fail without a request and are left out. WithtokenStore(e.g.readCookieTokenStore()), the player's data goes in the page HTML: only prefetch user data on pages that are never cached and served to other users. - Player data in a Client Component: only after hydration. The browser factories build the key with the browser client's session, which does not exist on SSR (
'anonymous'key, even with an authenticatedinitialSessionin theEnginexProvider): rendering the query on SSR breaks hydration. Gate the component on the authenticateduseSession()(with the defaultinitialSession); it mounts right after hydration and uses the hydrated data without a request. For the data in the HTML, use a Server Component. - Refetch in the browser: the dehydrated state carries only the key and the data, not the
staleTime. What decides whether the browser fetches again right on mount is the browserQueryClient'sstaleTime: use one above zero (the players use 5 min). TheQueryClientthat theEnginexProvidercreates, when the app does not pass its own, already uses 60 s. Theconfig.*factories carry their ownstaleTime.
Parameters
source
The core client or the factories of a product package.
select
(queries) => Selected
Receives the factories and returns the queries to fetch.
Returns
Promise<DehydratedState>
The dehydrated state, for the HydrationBoundary's state.
Throws
CONFIGURATION_ERROR (rejected promise), without a request, if source is the object of a product instead of its factories, or if select returns something that is not an SDK factory.
Examples
// app/layout.tsx (Server Component)
import { HydrationBoundary } from '@tanstack/react-query'
import { getEnginexServer, prefetchEnginex } from '@getenginex/sdk-next/server'
export default async function Layout({ children }: { children: React.ReactNode }) {
const enginex = await getEnginexServer({ next: { revalidate: 300 } })
const state = await prefetchEnginex(enginex, (q) => [q.config.get(), q.config.getProducts()])
return <HydrationBoundary state={state}>{children}</HydrationBoundary>
}// Factories of a product package
import { createIgaming } from '@getenginex/sdk-igaming'
import { createIgamingQueries } from '@getenginex/sdk-igaming/query'
const igaming = createIgaming(await getEnginexServer())
const state = await prefetchEnginex(createIgamingQueries(igaming), (ig) => [ig.casino.listGames({ page: 1 })])Call Signature
prefetchEnginex<
Q>(source,select):Promise<DehydratedState>
Prefetch of SDK queries in a Server Component, to hydrate the browser QueryClient with TanStack Query's <HydrationBoundary state={...}>, with the same keys the factories use on the client.
source: the server client (await getEnginexServer();selectreceives the core factories) or the factories of a product package (createIgamingQueries(igaming);selectreceives them as they came).- One
QueryClientper call: created here and discarded at the end; nothing stays between calls or requests. No retry on the server. - Failures: a factory that fails (gateway error, user query without a session) does not take down the others and stays out of the state; only successful queries are dehydrated (never an error). The browser fetches those normally.
- Session: with an anonymous client (the
getEnginexServerdefault), user queries fail without a request and are left out. WithtokenStore(e.g.readCookieTokenStore()), the player's data goes in the page HTML: only prefetch user data on pages that are never cached and served to other users. - Player data in a Client Component: only after hydration. The browser factories build the key with the browser client's session, which does not exist on SSR (
'anonymous'key, even with an authenticatedinitialSessionin theEnginexProvider): rendering the query on SSR breaks hydration. Gate the component on the authenticateduseSession()(with the defaultinitialSession); it mounts right after hydration and uses the hydrated data without a request. For the data in the HTML, use a Server Component. - Refetch in the browser: the dehydrated state carries only the key and the data, not the
staleTime. What decides whether the browser fetches again right on mount is the browserQueryClient'sstaleTime: use one above zero (the players use 5 min). TheQueryClientthat theEnginexProvidercreates, when the app does not pass its own, already uses 60 s. Theconfig.*factories carry their ownstaleTime.
Type Parameters
Q
Q extends object
Parameters
source
Q
The core client or the factories of a product package.
select
(queries) => Selected
Receives the factories and returns the queries to fetch.
Returns
Promise<DehydratedState>
The dehydrated state, for the HydrationBoundary's state.
Throws
CONFIGURATION_ERROR (rejected promise), without a request, if source is the object of a product instead of its factories, or if select returns something that is not an SDK factory.
Examples
// app/layout.tsx (Server Component)
import { HydrationBoundary } from '@tanstack/react-query'
import { getEnginexServer, prefetchEnginex } from '@getenginex/sdk-next/server'
export default async function Layout({ children }: { children: React.ReactNode }) {
const enginex = await getEnginexServer({ next: { revalidate: 300 } })
const state = await prefetchEnginex(enginex, (q) => [q.config.get(), q.config.getProducts()])
return <HydrationBoundary state={state}>{children}</HydrationBoundary>
}// Factories of a product package
import { createIgaming } from '@getenginex/sdk-igaming'
import { createIgamingQueries } from '@getenginex/sdk-igaming/query'
const igaming = createIgaming(await getEnginexServer())
const state = await prefetchEnginex(createIgamingQueries(igaming), (ig) => [ig.casino.listGames({ page: 1 })])