Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions app/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import { NextRequest, NextResponse } from 'next/server'

const VARIANTS = ['a', 'b', 'c', 'd', 'e'] as const
const THEMES = ['baseline', 'operator-dark', 'operator-light', 'library'] as const
// Live A/B copy test: two finalists, both in the impeccable design, so COPY is
// the only variable.
// bi → /bi (Version B copy: outcome-first + the 80-second Odoo demo)
// di → /di (Version D copy: show-the-magic + L1/L2/L3 escalation)
// The cream A/C and the old cream B/D + the /e variant are retired from the
// random rotation (still reachable by direct URL). The /bi and /di pages have a
// fixed design and ignore the theme switcher, so no theme rotation here.
const VARIANTS = ['bi', 'di'] as const
const VARIANT_COOKIE = 'kc-landing-variant'
const THEME_COOKIE = 'kc-landing-theme'

export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl

if (pathname !== '/') return NextResponse.next()

const existingVariant = request.cookies.get(VARIANT_COOKIE)?.value
const existingTheme = request.cookies.get(THEME_COOKIE)?.value

const variant = VARIANTS.includes(existingVariant as any)
? existingVariant!
const existing = request.cookies.get(VARIANT_COOKIE)?.value
const variant = VARIANTS.includes(existing as any)
? existing!
: VARIANTS[Math.floor(Math.random() * VARIANTS.length)]

const theme = THEMES.includes(existingTheme as any)
? existingTheme!
: THEMES[Math.floor(Math.random() * THEMES.length)]

const maxAge = 60 * 60 * 24 * 30

if (variant === 'a') {
const response = NextResponse.next()
if (!existingVariant) response.cookies.set(VARIANT_COOKIE, variant, { maxAge, path: '/' })
if (!existingTheme) response.cookies.set(THEME_COOKIE, theme, { maxAge, path: '/' })
return response
}

const url = request.nextUrl.clone()
url.pathname = `/${variant}`
const response = NextResponse.rewrite(url)

if (!existingVariant) response.cookies.set(VARIANT_COOKIE, variant, { maxAge, path: '/' })
if (!existingTheme) response.cookies.set(THEME_COOKIE, theme, { maxAge, path: '/' })
if (!VARIANTS.includes(existing as any)) {
response.cookies.set(VARIANT_COOKIE, variant, {
maxAge: 60 * 60 * 24 * 30,
path: '/',
})
}

return response
}
Expand Down