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
62 lines
1.5 KiB
Plaintext
62 lines
1.5 KiB
Plaintext
---
|
|
title: Removed page from Proxy API
|
|
---
|
|
|
|
## Why This Error Occurred
|
|
|
|
Your application is interacting with `request.page` which has been deprecated.
|
|
|
|
```ts filename="proxy.ts"
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { params } = request.page
|
|
const { locale, slug } = params
|
|
|
|
if (locale && slug) {
|
|
const { search, protocol, host } = request.nextUrl
|
|
const url = new URL(`${protocol}//${locale}.${host}/${slug}${search}`)
|
|
return NextResponse.redirect(url)
|
|
}
|
|
}
|
|
```
|
|
|
|
## Possible Ways to Fix It
|
|
|
|
You can use [URLPattern](https://developer.mozilla.org/docs/Web/API/URLPattern) instead to have the same behavior:
|
|
|
|
```ts filename="proxy.ts"
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
const PATTERNS = [
|
|
[
|
|
new URLPattern({ pathname: '/:locale/:slug' }),
|
|
({ pathname }) => pathname.groups,
|
|
],
|
|
]
|
|
|
|
const params = (url) => {
|
|
const input = url.split('?')[0]
|
|
let result = {}
|
|
|
|
for (const [pattern, handler] of PATTERNS) {
|
|
const patternResult = pattern.exec(input)
|
|
if (patternResult !== null && 'pathname' in patternResult) {
|
|
result = handler(patternResult)
|
|
break
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const { locale, slug } = params(request.url)
|
|
|
|
if (locale && slug) {
|
|
const { search, protocol, host } = request.nextUrl
|
|
const url = new URL(`${protocol}//${locale}.${host}/${slug}${search}`)
|
|
return NextResponse.redirect(url)
|
|
}
|
|
}
|
|
```
|