guide

Server-Side Rendering (SSR)

Use postbasejs in Next.js Server Components, middleware, and client components with session persistence.

Published: March 22, 2026Updated: March 22, 2026

Server-Side Rendering (SSR)

Import from postbasejs/ssr to get cookie-aware clients for Next.js.

Server Component / Middleware

import { createServerClient } from 'postbasejs/ssr'

const postbase = createServerClient(url, anonKey, {
  cookies: {
    getAll: () => cookieStore.getAll(),
    setAll: (cookies) =>
      cookies.forEach(c => res.cookies.set(c.name, c.value, c.options)),
  },
})

Client Component

import { createBrowserClient } from 'postbasejs/ssr'

const postbase = createBrowserClient(url, anonKey)

The session is stored in a cookie named postbase-session. The SSR client automatically forwards it as the X-Postbase-Session header on each API request, so server-side queries run with the correct RLS context.