You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are defining a middleware file in a location different from `pages/_middleware` which is _deprecated_.
6
+
7
+
Declaring a middleware file under specific pages brought the illusion that a middleware would be executed _only_ when pages below its declaration were matched but this wasn't the case.
8
+
Supporting this API also comes with other consequences like allowing to nest multiple middleware or dragging effects between different middleware executions.
9
+
10
+
This makes the execution model for middleware **really complex** and hard to reason about so this API is _deprecated_ in favour of a simpler model with a single root middleware.
11
+
12
+
#### Possible Ways to Fix It
13
+
14
+
To fix this error you must declare your middleware in the root pages folder and leverage `NextRequest` parsed URL to decide wether or not to execute the middleware code.
15
+
For example, a middleware declared under `pages/about/_middleware.js` can be moved to `pages/_middleware` and updated so that it only executes when `about/*` matches:
16
+
17
+
```typescript
18
+
importtype { NextRequest } from'next/server'
19
+
20
+
exportfunction middleware(request:NextRequest) {
21
+
if (request.nextUrl.pathname.startsWith('/about')) {
22
+
// Execute pages/about/_middleware.js
23
+
}
24
+
}
25
+
```
26
+
27
+
This also means that if you have more than one middleware you will need to reunite them in a single file and model their execution depending on the request.
0 commit comments