NextRequest
NextRequest は、追加の便利なメソッドと共に Web Request API を拡張します。
cookies
リクエストの Set-Cookie ヘッダーを読み取るか、変更します。
set(name, value)
指定された名前で、リクエストに指定された値のクッキーを設定します。
// 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)
クッキー名を指定すると、そのクッキーの値が返されます。クッキーが見つからない場合は undefined が返されます。複数のクッキーが見つかった場合は、最初のものが返されます。
// Given incoming request /home
// { name: 'show-banner', value: 'false', Path: '/home' }
request.cookies.get('show-banner')getAll()
クッキー名を指定すると、リクエストのクッキーの値が返されます。名前が指定されていない場合は、リクエストのすべてのクッキーが返されます。
// 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)
クッキー名を指定すると、リクエストからクッキーが削除されます。
// Returns true for deleted, false is nothing is deleted
request.cookies.delete('experiments')has(name)
クッキー名を指定すると、リクエストにクッキーが存在するかどうかを true で返します。
// Returns true if cookie exists, false if it does not
request.cookies.has('experiments')clear()
リクエストから Set-Cookie ヘッダーを削除します。
request.cookies.clear()nextUrl
追加の便利なメソッド(Next.js固有のプロパティを含む)で、ネイティブの URL API を拡張します。
// 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利用可能なオプションは以下の通りです。
| プロパティ | タイプ | 説明 | 
|---|---|---|
| basePath | string | URL の base path。 | 
| buildId | string|undefined | Next.js アプリケーションのビルド識別子。 カスタマイズ できます。 | 
| defaultLocale | string|undefined | 国際化 のデフォルトロケール。 | 
| domainLocale | ||
| - defaultLocale | string | ドメイン内のデフォルトロケール。 | 
| - domain | string | 特定のロケールに関連付けられたドメイン。 | 
| - http | boolean|undefined | ドメインが HTTP を使用しているかどうかを示します。 | 
| locales | string[]|undefined | 利用可能なロケールの配列。 | 
| locale | string|undefined | 現在アクティブなロケール。 | 
| url | URL | URL オブジェクト。 | 
バージョン履歴
| バージョン | 変更履歴 | 
|---|---|
| v15.0.0 | ipおよびgeoは削除されました。 | 
役に立ちましたか?