rsnext/errors/nested-middleware.mdx
Delba de Oliveira 44d1a1cb15
docs: Migrate error messages to MDX and App Router. (#52038)
This PR is part of a larger effort to migrate error messages to MDX and
use App Router: https://github.com/vercel/front/pull/23459
2023-07-05 06:11:16 -07:00

31 lines
1.3 KiB
Text

---
title: Nested Middleware
---
## Why This Error Occurred
You are defining a Middleware file in a location different from `<root>/middleware`, which is not allowed.
While in beta, a Middleware file under specific pages would _only_ be executed when pages below its declaration were matched, allowing nesting Middleware files. Based on customer feedback, we have replaced this API with a single root Middleware.
## Possible Ways to Fix It
Declare your Middleware in the root folder and use `NextRequest` parsed URL to define which path the Middleware should be executed for.
For example, a Middleware at `pages/about/_middleware.ts` can move the logic to `<root>/middleware.ts` in the root of your repository. Then, a conditional statement can be used to only run the Middleware when it matches the `about/*` path:
```ts filename="middleware.ts"
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/about')) {
// This logic is only applied to /about
}
if (request.nextUrl.pathname.startsWith('/dashboard')) {
// This logic is only applied to /dashboard
}
}
```
If you have more than one Middleware, you should combine them into a single file and model their execution depending on the incoming request.