consolelog.tools

Next.js Middleware Builder

Generate Next.js middleware for authentication, rate limiting, and more

About this ToolHow it works, benefits & use cases

Next.js middleware runs on every matching request before a page renders, which makes it the right place for auth gating, redirects, header injection, and logging — but the NextRequest/NextResponse API and the matcher config are easy to get wrong. This builder assembles a working middleware.ts from a checklist of features. Tick what you need — an authentication check that reads an auth-token cookie and redirects unauthenticated visitors to /login (with a from query param), a list of protected route prefixes, rate-limiting scaffolding, request logging, a www-to-non-www 301 redirect, security headers (X-Frame-Options, X-Content-Type-Options, Referrer-Policy), and locale detection — and it emits typed middleware plus a matcher config that excludes API routes, _next assets, and favicon by default. The output is correctly ordered (early returns for redirects, a response object when headers are added) and ready to drop in your project root. Everything generates in the browser, and your selection is captured in a shareable URL.

How to Use

  1. 1Toggle "Authentication check" on if you want route protection; the redirect logic reads an auth-token cookie.
  2. 2When auth is on, manage "Protected Routes": type a path prefix (for example /admin) and press Enter or the plus button, removing any with the trash icon.
  3. 3Enable the other features you need — "Rate limiting", "Request logging", "Redirect www to non-www", "Add security headers", and "Locale detection".
  4. 4Click "Generate Middleware" to build the file.
  5. 5Copy or download the result as middleware.ts into your project root, or share your configuration via its URL.

Key Benefits

  • Generates a typed middleware.ts using NextRequest/NextResponse, not hand-written boilerplate
  • Auth check reads an auth-token cookie and redirects to /login with the original path as a from param
  • Protected routes are matched by prefix, so /dashboard also covers /dashboard/settings
  • Optional security headers: X-Frame-Options, X-Content-Type-Options, and Referrer-Policy
  • www-to-non-www 301 redirect, request logging, locale detection, and rate-limit scaffolding as toggles
  • Includes a matcher config that skips API routes, _next static and image assets, and favicon
  • Runs in the browser with a shareable URL that captures your selected features

Common Use Cases

  • Gating /dashboard, /profile, or /admin behind a session cookie
  • Adding baseline security headers across an entire Next.js app from one file
  • Forcing a canonical host by redirecting www to the apex domain
  • Logging method and pathname for every request during development
  • Bootstrapping a middleware skeleton you will extend with real rate limiting

Middleware Features

Protected Routes

/dashboard
/profile
Run to see the generated Next.js middleware.

Middleware Tips

  • Place middleware.ts in the root of your project
  • Middleware runs before every request
  • Use matcher config to limit which routes are affected
  • Keep middleware lightweight for better performance

Was this tool helpful?

Share Your Experience

Help others discover this tool!

Related tools