Install · runtime

The agent on Next.js

Before the response. In Next.js the agent is the middleware file, App Router or Pages Router alike.

How it works on Next.js

In Next.js the agent is the middleware file, App Router or Pages Router alike. On Vercel or self-hosted, the file is the same.

The file

middleware.ts — At your project root, beside app/ or pages/.

import { NextResponse } from 'next/server'
import { reflexa } from './reflexa-agent.js'

const agent = reflexa({ token: process.env.REFLEXA_TOKEN })

export async function middleware(request) {
  try {
    return (await agent.ahead(request)) ?? NextResponse.next()
  } catch {
    return NextResponse.next()
  }
}

export const config = { matcher: '/:path*' }

Get reflexa-agent.js and the site's token from the dashboard's Agent page, put the token in REFLEXA_TOKEN, deploy. Then press Check the connection in the dashboard: it fetches the domain proof from your real domain and the agent is live.

What you can measure

  • records AI crawler requests and referrals by page
  • verifies the site connection using the domain proof

Go deeper on reflexatechnologies.com: the Next.js guide · check your site's AI crawler access first (free)

All install guides → · Data and privacy →

Questions people ask

Do I need to connect my site for the first check?

A public URL is enough for the external diagnosis. A supported site connection adds AI request and referral data.

Can I take it out?

Remove the integration from the runtime and redeploy to stop reporting new traffic. Your saved reports remain in the dashboard.

Which data does this connection add?

Records AI crawler requests and referrals by page. Verifies the site connection using the domain proof.

Is the token a secret?

Yes. It belongs to this one site and should live in an environment variable, never in a file you commit. It grants nothing beyond this site, and the domain proof refuses it from any other host.

Find your next search opportunity.

Get your free report with 40+ checks. See which pages need attention, why they matter and where to start.

Create a free account in the next step for the full diagnosis.