Unverified Commit 11c906a7 authored by Hichem's avatar Hichem Committed by GitHub

update proxy docs to use NextProxy type (#90647)

### What?
Updated all proxy code examples in the API reference to use the
`NextProxy` typed arrow function pattern, consistent with the convention
used in `src/proxy.ts`.

### Why?
The proxy function is typed as `NextProxy` so parameters are inferred
from the type, removing the need for explicit `NextRequest` imports.

### How?
Updated all proxy code examples in the API reference to use the
`NextProxy` typed arrow function pattern, consistent with the convention
used in `src/proxy.ts`.

---------
Co-authored-by: 's avatarJoseph <sephxd1234@gmail.com>
Co-authored-by: 's avatarJoseph <joseph.chamochumbi@vercel.com>
parent 18b94630
......@@ -25,7 +25,8 @@ Create a `proxy.ts` (or `.js`) file in the project root, or inside `src` if appl
If you’ve customized [`pageExtensions`](/docs/app/api-reference/config/next-config-js/pageExtensions), for example to `.page.ts` or `.page.js`, name your file `proxy.page.ts` or `proxy.page.js` accordingly.
```tsx filename="proxy.ts" switcher
import { NextResponse, NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export function proxy(request: NextRequest) {
......@@ -151,6 +152,17 @@ export function proxy(request) {
}
```
If you prefer a shorthand, you can use the `NextProxy` type. It infers the parameter types for both `request` (`NextRequest`) and `event` (`NextFetchEvent`) automatically:
```tsx filename="proxy.ts"
import type { NextProxy } from 'next/server'
export const proxy: NextProxy = (request, event) => {
event.waitUntil(Promise.resolve())
return Response.json({ pathname: request.nextUrl.pathname })
}
```
> **Good to know**:
>
> - `NextRequest` is a type that represents incoming HTTP requests in Next.js Proxy, whereas [`NextResponse`](#nextresponse) is a class used to manipulate and send back HTTP responses.
......@@ -422,7 +434,8 @@ Learn more in [NextResponse headers in Proxy](/docs/app/api-reference/functions/
You can set CORS headers in Proxy to allow cross-origin requests, including [simple](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) and [preflighted](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests) requests.
```tsx filename="proxy.ts" switcher
import { NextRequest, NextResponse } from 'next/server'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const allowedOrigins = ['https://acme.com', 'https://my-app.org']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment