Unverified Commit 4bf9d910 authored by Joseph's avatar Joseph Committed by GitHub

docs: forward allow-listed headers and catch fetch errors in BFF proxy example (#100808)

## Summary

The "Proxying to a backend" example in the Backend for Frontend guide
built the upstream request with `new Request(proxyURL, request)`, which
forwards every incoming header, including `Cookie` and `Authorization`,
to the external backend. The example now forwards an allow-list
(`accept`, `content-type`), and the intro says why.

It also returned `fetch()` without `await` inside `try`, so a rejected
fetch skipped the `catch` and the 500 fallback never ran. It now uses
`return await fetch(proxyRequest)`.

- docs: forward allow-listed headers and catch fetch errors in BFF proxy
example

<!-- NEXT_JS_LLM -->
parent 8f7dd001
...@@ -442,7 +442,7 @@ export async function POST(request) { ...@@ -442,7 +442,7 @@ export async function POST(request) {
## Proxying to a backend ## Proxying to a backend
You can use a Route Handler as a `proxy` to another backend. Add validation logic before forwarding the request. You can use a Route Handler as a `proxy` to another backend. Add validation logic before forwarding the request, and forward only the headers the backend needs. Copying every incoming header also sends the user's `Cookie` and `Authorization` values, so forward those only if the backend expects them.
```ts filename="/app/api/[...slug]/route.ts" switcher ```ts filename="/app/api/[...slug]/route.ts" switcher
import { isValidRequest } from '@/lib/utils' import { isValidRequest } from '@/lib/utils'
...@@ -458,10 +458,18 @@ export async function POST(request: Request, { params }) { ...@@ -458,10 +458,18 @@ export async function POST(request: Request, { params }) {
const { slug } = await params const { slug } = await params
const pathname = slug.join('/') const pathname = slug.join('/')
const proxyURL = new URL(pathname, 'https://nextjs.org') const proxyURL = new URL(pathname, 'https://nextjs.org')
const proxyRequest = new Request(proxyURL, request)
const headers = new Headers()
for (const name of ['accept', 'content-type']) {
const value = request.headers.get(name)
if (value) headers.set(name, value)
}
// The inner Request keeps the method and body; the outer one replaces the headers
const proxyRequest = new Request(new Request(proxyURL, request), { headers })
try { try {
return fetch(proxyRequest) return await fetch(proxyRequest)
} catch (reason) { } catch (reason) {
const message = const message =
reason instanceof Error ? reason.message : 'Unexpected exception' reason instanceof Error ? reason.message : 'Unexpected exception'
...@@ -485,10 +493,18 @@ export async function POST(request, { params }) { ...@@ -485,10 +493,18 @@ export async function POST(request, { params }) {
const { slug } = await params const { slug } = await params
const pathname = slug.join('/') const pathname = slug.join('/')
const proxyURL = new URL(pathname, 'https://nextjs.org') const proxyURL = new URL(pathname, 'https://nextjs.org')
const proxyRequest = new Request(proxyURL, request)
const headers = new Headers()
for (const name of ['accept', 'content-type']) {
const value = request.headers.get(name)
if (value) headers.set(name, value)
}
// The inner Request keeps the method and body; the outer one replaces the headers
const proxyRequest = new Request(new Request(proxyURL, request), { headers })
try { try {
return fetch(proxyRequest) return await fetch(proxyRequest)
} catch (reason) { } catch (reason) {
const message = const message =
reason instanceof Error ? reason.message : 'Unexpected exception' reason instanceof Error ? reason.message : 'Unexpected exception'
......
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