Skip to content

Next.js Middleware Matcher Error Fix

DodaTech Updated 2026-06-24 1 min read

In this tutorial, you'll learn about Next.js Middleware Matcher Error Fix. We cover key concepts, practical examples, and best practices.

The Problem

Error: Middleware matcher does not match any routes. Check your config.

The matcher in middleware config does not match any request paths.

Wrong

export const config = {
  matcher: '/dashboard', // Only matches exact /dashboard
}

This does not match /dashboard/settings or /dashboard/profile.

import { NextRequest, NextResponse } from 'next/server'

export function middleware(request: NextRequest) {
  const token = request.cookies.get('session')
  if (!token) {
    return NextResponse.redirect(new URL('/login', request.url))
  }
  return NextResponse.next()
}

export const config = {
  matcher: [
    '/dashboard/:path*', // Matches /dashboard and all sub-routes
    '/profile/:path*',
    '/((?!login|api|_next/static|favicon.ico).*)', // Exclude public routes
  ],
}

Expected output: middleware runs on all dashboard and profile routes, and redirects to login if no session cookie.

Prevention

  • Use :path* for matching routes with sub-paths
  • Use negative lookaheads to exclude routes
  • Test matchers with a variety of URLs in development

Common Mistakes with middleware matcher

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world NEXTJS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the syntax for Next.js middleware matchers?

Matchers use path-to-regexp syntax. :path* matches any path segment, (.*) matches everything, ((?!pattern).*) is a negative lookahead.

Can I have multiple matcher patterns?

Yes. Use an array of strings. Middleware runs if the request path matches any pattern in the array.

How do I match all routes except specific ones?

Use a negative lookahead: matcher: '/((?!api|_next/static).*)' runs middleware on all routes except /api and /_next/static.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro