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:
属性 | 类型 | 描述 |
---|---|---|
basePath | string | URL 的 基本路径。 |
buildId | string | undefined | Next.js 应用的构建标识符。可以是 customized。 |
defaultLocale | string | undefined | internationalization 的默认语言环境。 |
domainLocale | ||
* defaultLocale | string | 域名内的默认语言环境。 |
* domain | string | 与特定语言环境关联的域名。 |
* http | boolean | undefined | 指示域是否使用 HTTP。 |
locales | string[] | undefined | 可用语言环境的数组。 |
locale | string | undefined | 当前活动的语言环境。 |
url | URL | URL 对象。 |
版本历史
¥Version History
版本 | 更改 |
---|---|
v15.0.0 | 删除了 ip 和 geo 。 |