---
url: >-
  https://game-engine-io.github.io/enginex-frontend-sdk/reference/api/@getenginex/sdk-next/server/functions/prefetchEnginex.md
---
[Reference](../../../../index.md) / [@getenginex/sdk-next/server](../index.md) / 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()`; `select` receives the core factories)
  or the factories of a product package (`createIgamingQueries(igaming)`; `select` receives them as
  they came).
* **One `QueryClient` per 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 `getEnginexServer` default), user queries fail
  without a request and are left out. With `tokenStore` (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
  authenticated `initialSession` in the `EnginexProvider`): rendering the query on SSR breaks hydration.
  Gate the component on the authenticated `useSession()` (with the default `initialSession`); 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 browser `QueryClient`'s `staleTime`: use one
  above zero (the players use 5 min). The `QueryClient` that the `EnginexProvider` creates, when the app does not
  pass its own, already uses 60 s. The `config.*` factories carry their own `staleTime`.

### Parameters

#### source

[`EnginexClient`](../../../sdk/interfaces/EnginexClient.md)

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

```ts
// 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>
}
```

```ts
// 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()`; `select` receives the core factories)
  or the factories of a product package (`createIgamingQueries(igaming)`; `select` receives them as
  they came).
* **One `QueryClient` per 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 `getEnginexServer` default), user queries fail
  without a request and are left out. With `tokenStore` (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
  authenticated `initialSession` in the `EnginexProvider`): rendering the query on SSR breaks hydration.
  Gate the component on the authenticated `useSession()` (with the default `initialSession`); 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 browser `QueryClient`'s `staleTime`: use one
  above zero (the players use 5 min). The `QueryClient` that the `EnginexProvider` creates, when the app does not
  pass its own, already uses 60 s. The `config.*` factories carry their own `staleTime`.

### 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

```ts
// 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>
}
```

```ts
// 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 })])
```
