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:
Joseph <sephxd1234@gmail.com>
Showing
Please
register
or
sign in
to comment