コンテンツへスキップ

NextRequest

NextRequestは、Web Request APIに追加の便利なメソッドを拡張したものです。

cookies

リクエストのSet-Cookieヘッダーを読み取りまたは変更します。

set(name, value)

名前を指定して、リクエストに指定された値でCookieを設定します。

// 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が見つかった場合、最初に見つかったCookieが返されます。

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

getAll()

Cookie名を指定して、そのCookieの値を返します。名前が指定されていない場合、リクエスト上のすべてのCookieを返します。

// 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を削除します。

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

has(name)

Cookie名を指定して、そのCookieがリクエストに存在する場合、trueを返します。

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

clear()

リクエストからSet-Cookieヘッダーを削除します。

request.cookies.clear()

nextUrl

ネイティブのURL APIに追加の便利なメソッド(Next.js固有のプロパティを含む)を拡張したものです。

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

以下のオプションが利用可能です

プロパティ説明
basePathstringURLのベースパスです。
buildIdstring | undefinedNext.jsアプリケーションのビルド識別子です。カスタマイズ可能です。
pathnamestringURLのパス名です。
searchParamsObjectURLの検索パラメータです。

注: Pages Routerの国際化プロパティはApp Routerでは利用できません。App Routerでの国際化について詳しくはこちらをご覧ください。

バージョン履歴

バージョン変更点
v15.0.0ipおよびgeoは削除されました。