Skip to main content

Next.js Middleware Builder

Generate production-ready middleware.ts for Next.js — auth protection, redirects, A/B testing, and matcher config explained.

✓ Zero dependencies ✓ Auth + redirect + A/B ✓ Matcher config explained ✓ Edge Runtime compatible

Authentication Check

Protected Route Patterns

Requests matching these patterns require authentication.

Redirect Rules

A/B Testing

middleware.ts

 

Matcher Config Explanation

Frequently Asked Questions

What is Next.js middleware?▼

Next.js middleware runs before a request is processed, allowing you to modify the response, redirect users, rewrite URLs, or add headers — all at the Edge, before the page renders. It's defined in a middleware.ts file at the root of your project (or src/ directory). Middleware runs on every request that matches its config.matcher pattern.

What is the matcher config in Next.js middleware?▼

The matcher config in middleware.ts tells Next.js which paths to run middleware on. It supports regex-like patterns: '/((?!_next/static|_next/image|favicon.ico).*)' skips static assets. You can use arrays for multiple patterns. Matcher patterns must be static strings — no dynamic values or variables.

How do I protect routes with middleware?▼

Check authentication in middleware by reading a cookie (NextResponse.cookies.get), a header (request.headers.get), or a JWT in the Authorization header. If the user is not authenticated, call NextResponse.redirect() to send them to a login page. Always pass the request URL as the callbackUrl parameter so they return after login.

What is the difference between redirect and rewrite in middleware?▼

redirect changes the URL visible in the browser and sends a new request to the target path. rewrite proxies the request to a different path without changing the visible URL — the user sees the original URL but gets content from the rewrite target. Rewrites are useful for A/B testing, internationalization, and clean URL routing.

Can middleware access environment variables?▼

Yes, but with a caveat: middleware runs on the Edge Runtime, which is a restricted JavaScript environment. You can access process.env for server-side variables. However, you cannot use Node.js-specific APIs (fs, path, crypto built-ins). For JWT verification, use the Web Crypto API (crypto.subtle) or edge-compatible libraries like jose.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →