---
title: "A/B Testing a Next.js Landing Page When You're Using Clerk"
description: "Clerk's middleware needs to run on every matched route, same as most A/B testing middleware. Here's how to run a test without the two fighting each other."
canonical: https://landtest.dev/blog/ab-testing-nextjs-clerk-auth
last-updated: 2026-10-30
---

# A/B Testing a Next.js Landing Page When You're Using Clerk

> Clerk's middleware needs to run on every matched route, same as most A/B testing middleware. Here's how to run a test without the two fighting each other.

A/B testing a Next.js landing page when Clerk handles auth requires either composing the test's middleware logic carefully with `clerkMiddleware`, or avoiding middleware for the test entirely. Clerk's middleware needs to run on every matched route to enforce your auth rules, and if a second middleware concern — an A/B test rewrite — is added without explicit ordering, you risk the test firing for authenticated users who should never see a landing-page variant, or the auth check and the rewrite interfering with each other's headers.

## Why this specifically trips people up

[Clerk's middleware documentation](https://clerk.com/docs/references/nextjs/clerk-middleware) is explicit that `clerkMiddleware()` needs to wrap your route matching logic. If you're also running a middleware-based A/B test (GrowthBook, a hand-rolled cookie bucket, Vercel Edge Config), you now have two concerns that both want to intercept the same request, and Next.js middleware only runs one function per matched path — composition has to be deliberate.

## Compose explicitly, and exclude authenticated users

The safest pattern runs Clerk's auth check first and only applies the A/B test logic for unauthenticated (marketing page) traffic:

```ts
// middleware.ts
import { clerkMiddleware } from "@clerk/nextjs/server";
import { assignVariant } from "./lib/experiment";

export default clerkMiddleware((auth, req) => {
  if (auth().userId) return; // logged-in users skip the test entirely
  return assignVariant(req);
});
```

This keeps the test scoped to marketing/landing pages that logged-out visitors see, and avoids the test rewrite ever touching an authenticated session's routing.

## Scope the matcher tightly

Make sure your middleware `matcher` config isn't broader than it needs to be — an A/B test rewrite firing on routes that also need Clerk's protection (app pages, API routes) is a common source of confusing redirect loops. Scope the test's rewrite matcher specifically to the landing page paths under test, not a broad catch-all.

## Or avoid middleware entirely

If composing these two concerns feels fragile — and for a team that didn't design the auth middleware to anticipate a second concern, it often is — the alternative is running the A/B test split outside `middleware.ts` altogether, at the deploy/hosting layer instead. This is how Landtest avoids the composition problem: your Clerk middleware stays exactly as it is today, untouched by the test, because the split happens before any Next.js request handler runs. See [A/B testing without middleware conflicts](/blog/ab-test-nextjs-landing-page-without-middleware-conflicts) for the broader pattern.

## FAQ

**Does Clerk conflict with A/B testing tools?**
Not inherently, but both want to run on the same requests via middleware, so they need to be composed deliberately — auth first, test logic scoped to exclude authenticated users.

**Can I run an A/B test on a page Clerk protects?**
Generally you shouldn't need to — A/B tests are typically for public marketing pages. If you do need to test something behind auth, treat it as a separate, higher-scrutiny case rather than routine landing page testing.

**What's the simplest way to avoid middleware conflicts with Clerk?**
Run the split outside `middleware.ts` entirely, at the deploy/hosting layer — Clerk's middleware then never has to share execution with test logic.

---

Related: [A/B test without middleware conflicts](/blog/ab-test-nextjs-landing-page-without-middleware-conflicts) · [Best A/B testing tools for Next.js developers](/blog/best-ab-testing-tools-nextjs-developers) · [Landtest for A/B testing](/ab-testing)
