By A/B testing directly on the server-side, you'll reduce layout shift from client-loaded experiments and improving your site's performance with smaller JavaScript bundles.
https://edge-functions-ab-testing-simple.vercel.sh
Since the different variants are generated statically on the server side, it mitigates any potential layout shift that could happen when a variant is inserted into the DOM client side, hence improving your site's performance.
The magic happens in the _middleware.ts
file:
import { NextRequest, NextResponse } from 'next/server'
import { getBucket } from '@lib/ab-testing'
import { HOME_BUCKETS } from '@lib/buckets'
const COOKIE_NAME = 'bucket-home'
export function middleware(req: NextRequest) {
// Get the bucket cookie
const bucket = req.cookies[COOKIE_NAME] || getBucket(HOME_BUCKETS)
const res = NextResponse.rewrite(`/home/${bucket}`)
// Add the bucket to cookies if it's not there
if (!req.cookies[COOKIE_NAME]) {
res.cookie(COOKIE_NAME, bucket)
}
return res
}
Deploy the example using Vercel:
Execute create-next-app
with npm or Yarn to bootstrap the example:
npx create-next-app --example https://github.com/vercel/examples/tree/main/edge-functions/ab-testing-simple ab-testing-simple
# or
yarn create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/ab-testing-simple ab-testing-simple
Next, run Next.js in development mode:
npm install
npm run dev
# or
yarn
yarn dev
Deploy it to the cloud with Vercel (Documentation).