Menu

userAgent

userAgentヘルパーはWeb Request APIを拡張し、リクエストからユーザーエージェントオブジェクトを操作するための追加のプロパティとメソッドを提供します。

middleware.ts
TypeScript
import { NextRequest, NextResponse, userAgent } from 'next/server'
 
export function middleware(request: NextRequest) {
  const url = request.nextUrl
  const { device } = userAgent(request)
 
  // device.typeは 'mobile'、'tablet'、'console'、'smarttv'、
  // 'wearable'、'embedded'、または undefined(デスクトップブラウザの場合)になります
  const viewport = device.type || 'desktop'
 
  url.searchParams.set('viewport', viewport)
  return NextResponse.rewrite(url)
}

isBot

リクエストが既知のボットからのものかどうかを示すブール値です。

browser

リクエストで使用されているブラウザに関する情報を含むオブジェクトです。

  • name: ブラウザの名前を表す文字列、または識別できない場合はundefined
  • version: ブラウザのバージョンを表す文字列、またはundefined

device

リクエストで使用されているデバイスに関する情報を含むオブジェクトです。

  • model: デバイスのモデルを表す文字列、またはundefined
  • type: デバイスのタイプを表す文字列。consolemobiletabletsmarttvwearableembedded、またはundefinedなどの値があります。
  • vendor: デバイスのベンダーを表す文字列、またはundefined

engine

ブラウザのエンジンに関する情報を含むオブジェクトです。

  • name: エンジンの名前を表す文字列。可能な値は次のとおりです:AmayaBlinkEdgeHTMLFlowGeckoGoannaiCabKHTMLLinksLynxNetFrontNetSurfPrestoTasmanTridentw3mWebKitまたはundefined
  • version: エンジンのバージョンを表す文字列、またはundefined

os

オペレーティングシステムに関する情報を含むオブジェクトです。

  • name: OSの名前を表す文字列、またはundefined
  • version: OSのバージョンを表す文字列、またはundefined

cpu

CPU アーキテクチャに関する情報を含むオブジェクトです。

  • architecture: CPUのアーキテクチャを表す文字列。可能な値は次のとおりです:68kamd64armarm64armhfavria32ia64irixirix64mipsmips64pa-riscppcsparcsparc64またはundefined