Skip to 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.

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

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

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

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

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

ts
request.cookies.clear()

nextUrl

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

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

ts
// 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
defaultLocalestring | undefinedinternationalization 的默认语言环境。
domainLocale
* defaultLocalestring域名内的默认语言环境。
* domainstring与特定语言环境关联的域名。
* httpboolean | undefined指示域是否使用 HTTP。
localesstring[] | undefined可用语言环境的数组。
localestring | undefined当前活动的语言环境。
urlURLURL 对象。

版本历史

¥Version History

版本更改
v15.0.0删除了 ipgeo