mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
71ce95dffa
> [!NOTE] > Best reviewed by each commit for better diff view. This PR clones the Middleware docs for Proxy and removes the Middleware docs. Did not clone the list of docs: - `errors/middleware-upgrade.mdx` - It's a middleware upgrade guide from v12.2 - `errors/beta-middleware.mdx` - It's an error when using middleware before v12.2 - `errors/returning-response-body.mdx` - Legacy behavior from versions < v12.2
32 lines
1.2 KiB
Plaintext
32 lines
1.2 KiB
Plaintext
---
|
|
title: Nested Proxy
|
|
---
|
|
|
|
## Why This Error Occurred
|
|
|
|
You are defining a Proxy file in a location different from `<root>/proxy`, which is not allowed.
|
|
|
|
While in beta, a Proxy file under specific pages would _only_ be executed when pages below its declaration were matched, allowing nesting Proxy files. Based on customer feedback, we have replaced this API with a single root Proxy.
|
|
|
|
## Possible Ways to Fix It
|
|
|
|
Declare your Proxy in the root folder and use `NextRequest` parsed URL to define which path the Proxy should be executed for.
|
|
|
|
For example, a Proxy at `pages/about/_proxy.ts` can move the logic to `<root>/proxy.ts` in the root of your repository. Then, a conditional statement can be used to only run the Proxy when it matches the `about/*` path:
|
|
|
|
```ts filename="proxy.ts"
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
export function proxy(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 Proxy, you should combine them into a single file and model their execution depending on the incoming request.
|