Skip to content

getAbTestingCookie

getAbTestingCookie reads the A/B Testing variant name stored in the request cookies.

Use it when rendering a page on the server so the same visitor keeps receiving the same variant.

import { cookies } from 'next/headers'
type CookieStore = Awaited<ReturnType<typeof cookies>>
const getAbTestingCookie = ({
slug,
locale,
cookieStore,
}: {
slug: string
locale: string
cookieStore: CookieStore
}): string | undefined
PropertyDefinition
slugThe page slug for the current request.
localeThe current locale.
cookieStoreThe cookie store returned by cookies().

In a Next.js App route, get the cookie store using cookies() from next/headers, then pass the variant name to fetchPage.

import { cookies } from 'next/headers'
import { fetchPage, getAbTestingCookie } from 'react-bricks/rsc'
const cookieStore = await cookies()
const variantName = getAbTestingCookie({
slug: cleanSlug,
locale,
cookieStore,
})
const page = await fetchPage({
slug: cleanSlug,
language: locale,
variantName,
config,
})