Skip to Content
DocsIntegration AdaptersNext.js Integration

Next.js Integration

Actyx RPC provides adapters designed to bridge the gap between Next.js request metadata (Server Actions / Route Handlers) and your procedure contexts.


nextAdapter()

Use nextAdapter() within your context generator to automatically extract request properties (like client IPs, hosts, referrers, and user agents):

import { createProcedure } from "@explita/actyx-rpc"; import { nextAdapter } from "@explita/actyx-rpc/adapters/next"; const procedure = createProcedure({ async createContext() { const { ip, host, pathname, userAgent, browser, platform, cookies } = await nextAdapter(); return { ok: true, ctx: { ip, host, pathname, userAgent, browser, platform, }, }; }, });

Returned Context Fields

FieldSource Header(s)Description
ipx-forwarded-forClient IP address.
hostx-forwarded-hosthostRequested hostname.
originoriginproto://hostRequest origin URL.
refererrefererReferrer URL.
pathnamex-pathname (middleware)Pathname (requires middleware setup).
searchParamsx-search-params (middleware)Query parameters object (requires middleware).
protox-forwarded-protoConnection protocol (http or https).
userAgentuser-agentFull browser user agent string.
browsersec-ch-uaParsed browser name and major version.
platformsec-ch-ua-platformUser operating system platform.
localeaccept-languageClient preferred locale string (e.g. en-US).
headersReadonly headers object.
cookiesReadonly request cookies.

Middleware Setup

Because Next.js Server Actions do not run in standard middleware contexts, request properties like pathname and searchParams are not directly visible to actions.

To make these fields available to nextAdapter(), add a proxy middleware (middleware.ts or proxy.ts) to inject proxy headers:

// middleware.ts or proxy.ts import { NextRequest, NextResponse } from "next/server"; export function middleware(request: NextRequest) { const url = request.nextUrl; const responseHeaders = { headers: { "x-pathname": url.pathname, "x-search-params": JSON.stringify(Object.fromEntries(url.searchParams)), }, }; return NextResponse.next(responseHeaders); }

[!NOTE] next is a peer dependency of @explita/actyx-rpc and is expected to be installed in your Next.js application environment.

Last updated on