Skip to main content

NextRequest

NextRequest 通过额外的便捷方法扩展了 网络请求 API

¥NextRequest extends the Web Request API with additional convenience methods.

cookies

读取或更改请求的 Set-Cookie 标头。

¥Read or mutate the Set-Cookie header of the request.

set(name, value)

给定名称,在请求上设置具有给定值的 cookie。

¥Given a name, set a cookie with the given value on the request.

// Given incoming request /home
// Set a cookie to hide the banner
// request will have a `Set-Cookie:show-banner=false;path=/home` header
request.cookies.set('show-banner', 'false')

get(name)

给定 cookie 名称,返回 cookie 的值。如果未找到 cookie,则返回 undefined。如果找到多个 cookie,则返回第一个。

¥Given a cookie name, return the value of the cookie. If the cookie is not found, undefined is returned. If multiple cookies are found, the first one is returned.

// Given incoming request /home
// { name: 'show-banner', value: 'false', Path: '/home' }
request.cookies.get('show-banner')

getAll()

给定 cookie 名称,返回 cookie 的值。如果未给出名称,则返回请求中的所有 cookie。

¥Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the request.

// Given incoming request /home
// [
// { name: 'experiments', value: 'new-pricing-page', Path: '/home' },
// { name: 'experiments', value: 'winter-launch', Path: '/home' },
// ]
request.cookies.getAll('experiments')
// Alternatively, get all cookies for the request
request.cookies.getAll()

delete(name)

给定 cookie 名称,从请求中删除 cookie。

¥Given a cookie name, delete the cookie from the request.

// Returns true for deleted, false is nothing is deleted
request.cookies.delete('experiments')

has(name)

给定 cookie 名称,如果请求中存在 cookie,则返回 true

¥Given a cookie name, return true if the cookie exists on the request.

// Returns true if cookie exists, false if it does not
request.cookies.has('experiments')

clear()

从请求中删除 Set-Cookie 标头。

¥Remove the Set-Cookie header from the request.

request.cookies.clear()

nextUrl

使用额外的便捷方法(包括 Next.js 特定属性)扩展原生 URL API。

¥Extends the native URL API with additional convenience methods, including Next.js specific properties.

// 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

可以使用以下选项:

¥The following options are available:

属性类型描述
basePathstringURL 的 基本路径
buildIdstring | undefinedNext.js 应用的构建标识符。可以是 customized
pathnamestringURL 的路径名。
searchParamsObjectURL 的搜索参数。

注意:Pages Router 中的国际化属性不可在 App Router 中使用。了解有关 使用 App Router 实现国际化 的更多信息。

¥Note: The internationalization properties from the Pages Router are not available for usage in the App Router. Learn more about internationalization with the App Router.

版本历史

¥Version History

版本变化
v15.0.0ipgeo 已删除。