middleware

Intercept and modify Next.js requests with middleware.ts before routes.

Updated Apr 10, 2026
One-click install
npx skills add https://github.com/theslashdojo/dojo --skill middleware-theslashdojo
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: middleware
Source: https://github.com/theslashdojo/dojo/tree/main/nodes/nextjs/middleware
Command: npx skills add https://github.com/theslashdojo/dojo --skill middleware-theslashdojo

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

Intercept and modify requests before they reach routes in Next.js to enforce authentication checks, routing decisions, and header policies at the edge.

Core Features & Use Cases

  • Authentication guards to protect routes and API endpoints.
  • Redirects, rewrites, and locale detection to route users and content efficiently.
  • Global response headers and logging to improve security and observability.

Quick Start

Create a middleware.ts at the project root (or inside src/) and export a middleware function that uses NextRequest and NextResponse, then configure a matcher to scope the middleware to protected paths.

Frequently Asked Questions about middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I protect Next.js routes with authentication middleware?

Protect Next.js routes by intercepting requests with a root-level middleware.ts file. Use NextRequest and NextResponse to enforce authentication guards before traffic reaches your application routes.

What is the best way to handle redirects and rewrites in Next.js?

Handle redirects and rewrites in Next.js by modifying requests within middleware. This approach routes users and content efficiently at the edge before they hit the target routes.

Can I use Next.js middleware for locale detection and A/B testing?

Yes, you can use Next.js middleware for locale detection and A/B testing. The middleware intercepts incoming requests to determine user locale and route traffic dynamically before page rendering.

How does edge-runtime affect Next.js middleware performance?

Edge-runtime optimizes Next.js middleware performance by executing routing logic closer to users. This ensures secure, performant request interception for authentication and header policies globally.

How do I configure a matcher to scope Next.js middleware?

Configure a matcher in your Next.js middleware file to scope interception to specific paths. This limits authentication guards and rewrites to protected routes, preventing unnecessary request processing.