Unverified Commit 2aa96d02 authored by Devesh Asawa's avatar Devesh Asawa Committed by GitHub

docs: type proxy examples with NextRequest and fix cookie check (#100417)

### What?

- Fixes the parameter type in the `proxy.ts` rewrite and redirect
examples in `docs/01-app/02-guides/backend-for-frontend.mdx` from
`Request` to `NextRequest`.
- Fixes the parameter type in the "Prefetching links in Proxy" example
in `docs/01-app/03-api-reference/02-components/link.mdx` from `Request`
to `NextRequest`.
- Fixes a logic bug in the same `link.mdx` example where cookies were
read using `request.cookies.authToken` instead of
`request.cookies.has('authToken')`.

### Why?

- `request.nextUrl` only exists on `NextRequest`, not the standard Web
`Request`. In TypeScript, using `request: Request` results in type
errors such as `Property 'nextUrl' does not exist on type 'Request'` (6
errors across the three examples).
- `request.cookies` is a `RequestCookies` map, so property access
`request.cookies.authToken` is always `undefined`. The correct API is
`request.cookies.has('authToken')`. In the previous example, even
requests with the cookie were incorrectly routed to the public
dashboard.

### How?

- Added `import type { NextRequest } from 'next/server'` and typed
`request: NextRequest` in the TypeScript code snippets.
- Updated both TS and JS code snippets in `link.mdx` to use
`request.cookies.has('authToken')`.
- Tested in production mode with Turbopack on Windows:
- TypeScript build check: the 6 type errors are resolved and the check
passes.
  - `/dashboard` without cookie: routes to public dashboard.
- `/dashboard` with `authToken` cookie: now correctly routes to
authenticated dashboard (previously failed and routed to public).

---------
Co-authored-by: 's avatarJoseph <sephxd1234@gmail.com>
parent 10463c73
...@@ -721,12 +721,12 @@ export function proxy(request) { ...@@ -721,12 +721,12 @@ export function proxy(request) {
} }
``` ```
You can also proxy requests using `proxy`: You can also proxy requests using `proxy`. This example reads `request.nextUrl`, so `request` is typed as [`NextRequest`](#nextrequest-and-nextresponse):
```ts filename="proxy.ts" switcher ```ts filename="proxy.ts" switcher
import { NextResponse } from 'next/server' import { type NextRequest, NextResponse } from 'next/server'
export function proxy(request: Request) { export function proxy(request: NextRequest) {
if (request.nextUrl.pathname === '/proxy-this-path') { if (request.nextUrl.pathname === '/proxy-this-path') {
const rewriteUrl = new URL('https://nextjs.org') const rewriteUrl = new URL('https://nextjs.org')
return NextResponse.rewrite(rewriteUrl) return NextResponse.rewrite(rewriteUrl)
...@@ -748,9 +748,9 @@ export function proxy(request) { ...@@ -748,9 +748,9 @@ export function proxy(request) {
Another type of response `proxy` can produce are redirects: Another type of response `proxy` can produce are redirects:
```ts filename="proxy.ts" switcher ```ts filename="proxy.ts" switcher
import { NextResponse } from 'next/server' import { type NextRequest, NextResponse } from 'next/server'
export function proxy(request: Request) { export function proxy(request: NextRequest) {
if (request.nextUrl.pathname === '/v1/docs') { if (request.nextUrl.pathname === '/v1/docs') {
request.nextUrl.pathname = '/v2/docs' request.nextUrl.pathname = '/v2/docs'
return NextResponse.redirect(request.nextUrl) return NextResponse.redirect(request.nextUrl)
......
...@@ -979,11 +979,12 @@ For example, if you want to serve a `/dashboard` route that has authenticated an ...@@ -979,11 +979,12 @@ For example, if you want to serve a `/dashboard` route that has authenticated an
```ts filename="proxy.ts" switcher ```ts filename="proxy.ts" switcher
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function proxy(request: Request) { export function proxy(request: NextRequest) {
const nextUrl = request.nextUrl const nextUrl = request.nextUrl
if (nextUrl.pathname === '/dashboard') { if (nextUrl.pathname === '/dashboard') {
if (request.cookies.authToken) { if (request.cookies.has('authToken')) {
return NextResponse.rewrite(new URL('/auth/dashboard', request.url)) return NextResponse.rewrite(new URL('/auth/dashboard', request.url))
} else { } else {
return NextResponse.rewrite(new URL('/public/dashboard', request.url)) return NextResponse.rewrite(new URL('/public/dashboard', request.url))
...@@ -998,7 +999,7 @@ import { NextResponse } from 'next/server' ...@@ -998,7 +999,7 @@ import { NextResponse } from 'next/server'
export function proxy(request) { export function proxy(request) {
const nextUrl = request.nextUrl const nextUrl = request.nextUrl
if (nextUrl.pathname === '/dashboard') { if (nextUrl.pathname === '/dashboard') {
if (request.cookies.authToken) { if (request.cookies.has('authToken')) {
return NextResponse.rewrite(new URL('/auth/dashboard', request.url)) return NextResponse.rewrite(new URL('/auth/dashboard', request.url))
} else { } else {
return NextResponse.rewrite(new URL('/public/dashboard', request.url)) return NextResponse.rewrite(new URL('/public/dashboard', request.url))
......
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