Capítulo 380 de 456

NextRequest

Core Idea

NextRequest extends the Web Request API with convenience methods for cookies and URL parsing, used primarily inside Proxy.

Key Concepts

  • request.cookies: set(name, value), get(name), getAll(name?), delete(name), has(name), clear() — manipulates the request's Cookie header.
  • request.nextUrl: extends native URL with basePath, buildId, defaultLocale, domainLocale (defaultLocale, domain, http), locales, locale, url.

Code Examples

// Given incoming request /home
request.cookies.set('show-banner', 'false')
  • O que demonstra: setting a cookie on the request (adds Set-Cookie header).
// Given a request to /home, pathname is /home
request.nextUrl.pathname
// Given a request to /home?name=lee, searchParams is { 'name': 'lee' }
request.nextUrl.searchParams
  • O que demonstra: reading pathname and query params via nextUrl.

Reference Tables

nextUrl propertyTypeDescription
basePathstringconfigured base path
buildIdstring | undefinedbuild identifier
defaultLocalestring | undefinedi18n default locale
localesstring[] | undefinedsupported locales
localestring | undefinedactive locale
urlURLunderlying URL object

Anti-patterns

  • Relying on ip/geo properties: removed in v15.0.0.

Key Takeaways

  1. NextRequest is primarily used inside Proxy (proxy.js) function signatures.
  2. Cookie methods mirror a Map-like API: get/getAll/set/delete/has/clear.
  3. nextUrl adds Next.js-specific routing/i18n context on top of the standard URL.

Connects To

  • NextResponse: the response-side counterpart with matching cookie API.
  • Proxy: primary consumer of NextRequest as its first function parameter.