Skip to content

Bot protection for Next.js App Router

Install @centinel/nextjs to check matched routes or single Route Handlers, wherever your Next.js app is hosted.

How it fits into Next.js

Package and requirements
@centinel/nextjs for Next.js 13+ with the App Router, React 18+ and Node.js 18+.
Two enforcement modes
createCentinelMiddlewareFromEnv() across matched routes, or isBot(request) inside specific Route Handlers.
Browser script
Wrap app/layout.tsx in <CentinelLayout siteKey={...}> to add browser signals to server validation.
Response routes
Blocked visitors go to /block and challenged visitors to /interstitial. Both pages live in your app.

How a request moves through Next.js

The middleware runs on matched routes before your page or Route Handler, so blocked requests never reach your code.

  1. Matcher

    Next.js runs middleware.ts, or proxy.ts on Next.js 16, only for paths in config.matcher, such as /api/:path* and /dashboard/:path*.

  2. Validator call

    The Centinel handler sends the request to the validator with CENTINEL_SECRET_KEY and waits up to 500 ms.

  3. Decision

    result.response continues the request on allow, redirects to /block on block and to /interstitial on challenge.

  4. Failure

    Timeouts and errors fail open. After repeated failures the middleware backs off from 1 s up to 5 minutes.

Minimal configuration and a live check

Protect matched routes

middleware.ts
import type { NextRequest } from 'next/server';
import { createCentinelMiddlewareFromEnv } from '@centinel/nextjs';

const centinel = createCentinelMiddlewareFromEnv();

export default async function middleware(request: NextRequest) {
  const result = await centinel(request);
  return result.response;
}

export const config = {
  matcher: ['/api/:path*', '/dashboard/:path*']
};

List only the routes you want checked in config.matcher. Unmatched routes and static files never reach the validator.

Run the dev server with debug output

Verify
DEBUG=centinel:* next dev

Load a protected route and look for Validation completed in Nms. Validator service responded with 401 means the secret key is wrong and requests fail open. In DevTools, confirm a _centinel cookie and the collector script.js request.

Deployment options

What to know before deploying

  • Next.js 16 ignores middleware.ts

    Next.js 16 replaced middleware.ts with proxy.ts, exporting proxy. A leftover middleware.ts is skipped at build time without an error, so protection stops.

  • Return result.response, not the handler result

    The handler resolves to a CentinelResult object, not a Response. Exporting it directly makes every matched request fail with a 500.

  • Create both /block and /interstitial

    The middleware redirects to these two paths, and neither is configurable in the current release. Add both pages before you enable enforcement.

  • Use isBot() from v1.2.3 or later

    Older releases returned true from isBot() on any validator error and had no timeout. Upgrade before you put manual validation on a login route.

  • Your hosting on Vercel, AWS, Docker or your own Node servers
  • Your authentication stack, such as NextAuth, Clerk or Auth0
  • Your React Server Components, database layer and payments
  • Routes outside your matcher

Next.js integration questions

Walk through your Next.js setup

Bring one route bots are hitting. We will map the right connector, timeouts, and log-only rollout for your Next.js stack.