Personalize server-rendered pages from your own proxy, with one cached Xenia call per visitor and page and base content on any failure.
Edge personalization is for a site that renders pages on its own server or edge, for example Next.js, a storefront proxy, or a server in front of your CMS. Your proxy knows which pages a journey targets and asks Xenia only for those, within a hard time budget. Each visitor's decision is cached for the session. A timeout, an error, or a missing manifest all serve base content, so Xenia can never slow down or break a page.
It serves journeys whose Personalization Trigger has Server-side pull channel
only switched on. With the switch off, use resolve()
instead. See Which integration does my journey need?
The edge helpers need a server-type SDK token. Keep it in your server's environment and never send it to the browser. Browser tokens are refused.
/, /women/* or uk.example.com/sale.@xenia-martech/sdk-js/next runs the whole flow in one call. It skips bots and
non-GET requests, sets the visitor cookie, reads consent, builds the standard
facts, calls Xenia, rewrites to the variant page and adds a Server-Timing header.
// proxy.ts
import type { NextRequest } from "next/server";
import { createXeniaEdge } from "@xenia-martech/sdk-js/edge";
import { xeniaEdgeProxy } from "@xenia-martech/sdk-js/next";
const edge = createXeniaEdge({
token: process.env.XENIA_SDK_TOKEN!,
manifestUrl: `${process.env.URL}/api/xenia/manifest`, // your own copy, below
});
// Or from XENIA_SDK_TOKEN, XENIA_URL, XENIA_EDGE_BUDGET_MS and XENIA_MANIFEST_URL:
// const edge = xeniaEdgeFromEnv();
export const proxy = (request: NextRequest) =>
xeniaEdgeProxy(request, {
edge,
variantSecret: process.env.XENIA_VARIANT_SECRET!, // the variant page verifies with it
skip: (path) => path.startsWith("/account"), // pages with no variant page
facts: (ctx) => ({ signed_in: Boolean(ctx.request.cookies.get("session")) }),
});Two route factories serve the manifest and keep it fresh:
// app/api/xenia/manifest/route.ts
import { createManifestRoute } from "@xenia-martech/sdk-js/next";
export const revalidate = 300;
export const GET = createManifestRoute({ token: process.env.XENIA_SDK_TOKEN, revalidate });// app/api/xenia/revalidate/route.ts — Xenia's storefront webhook
import { createRevalidateRoute } from "@xenia-martech/sdk-js/next";
export const POST = createRevalidateRoute({
secret: process.env.XENIA_WEBHOOK_SECRET!,
paths: ["/api/xenia/manifest"],
});The webhook regenerates your manifest the moment a journey changes. Point
manifestUrl at that route: its copy lists pages rather than content keys, so a
journey published since the last refresh is still decided.
To bundle the manifest at build time, add xenia-manifest public/xenia-manifest.json
as a prebuild script and pass the file as initialManifest. A cold instance then
never waits for it, and an unreachable Xenia never fails the build.
The page behind the rewrite reads the swaps back from the token with
decodeVariant(token, secret). A token that is malformed or not signed with your
secret returns null, so a crafted URL can never render arbitrary entries.
For a CMS page journey, personalizeVariantPage verifies the token, fetches the
page and applies every level of swaps in one call, with no Xenia call:
// app/xv/[token]/[[...slug]]/page.tsx — cached per variant
import { personalizeVariantPage } from "@xenia-martech/sdk-js/edge";
export const revalidate = 60;
export async function generateStaticParams() { return []; }
export default async function Page({ params }) {
const { token, slug } = await params;
const { content } = await personalizeVariantPage(token, { contentType: "page", fields: { slug } }, {
secret: process.env.XENIA_VARIANT_SECRET!,
cms, // contentfulAdapter or contentstackAdapter
contentKey: "home-main", // omit to apply every journey the token decided
});
if (!content) notFound();
return <PageView page={content} />;
}@xenia-martech/sdk-js/edge has no Next.js, React or DOM dependency. It runs in
Node, edge functions and workers.
import {
createXeniaEdge, deviceOf, edgeConsent, encodeVariant, isBot,
localTime, readTrail, swapGroups, viewerGeo, withView,
} from "@xenia-martech/sdk-js/edge";
const edge = createXeniaEdge({ token: process.env.XENIA_SDK_TOKEN!, budgetMs: 150 });
// per page request
if (isBot(req.headers.get("user-agent"))) return next();
const keys = await edge.match(host, path); // [] → not targeted
const { geo } = viewerGeo(req, { trust: ["x-vercel-ip-country"] }); // only your host's headers
const outcome = await edge.decide(keys ?? [], {
host, path, visitorId,
facts: { ...localTime(geo, new Date()), device: deviceOf(req.headers), ...(geo ? { geo } : {}) },
...(edgeConsent(req.headers).analytics
? { trail: withView(readTrail(cookies.get("xenia_trail")), path) }
: {}),
});
const groups = swapGroups(outcome.decisions);
if (groups.length) rewrite(`/xv/${await encodeVariant(groups, secret)}${path}`);A PHP storefront can use Xenia's PHP SDK, which routes the same pages to the edge automatically.
Your proxy sends the visitor's context with each decision:
geo.country, geo.city and so on. It must come from your edge,
because Xenia only sees your server. viewerGeo reads a location header only
when you list it in trust, since a visitor can send any header. Name the
ones your host sets, for example x-vercel-ip-country on Vercel or
cf-ipcountry behind Cloudflare.profile.* rules.createWeatherFacts({ unit }) adds temperature_now,
avg_temperature_next_7_days and weather_condition, never holding a decision
past its budget.edgeConsent reads the consent cookie the browser SDK writes:
Turn on the browsing signals in the browser client so decisions see the page the visitor just left:
<XeniaProvider pageViews pageLeave trail navClicks contentClicks {...config}>See Browsing signals.
Stamp each personalized section so the browser SDK reports impressions and clicks.
slotAttributes(slot) returns the attributes to spread on it. Exposures are
recorded by Xenia when it decides, so don't report them again.
Turn these on only in development or on a QA deploy:
xeniaEdgeProxy({ debug: true }) records each decision, and <XeniaDebugPanel />
from /client/react shows it in a small card: the decision per content key, the
journey path it took, the visitor's location and profile. Add your own rows with
debugInfo(info).xeniaEdgeProxy({ qa: true }) lets a tester place the visitor in a city
(?xn_geo=london) or decide as of a date (?xn_date=2026-12-24) for the rest of
the visit.Decisions are cached in each process's memory. On serverless or multi-region hosts,
share them with tieredDecisionCache(store) over your own store, for example Redis.
A store that is down or slower than 50 ms counts as a miss and never breaks a page.