跳到内容

userAgent

userAgent 助手扩展了 Web Request API,增加了额外的属性和方法,用于与请求中的用户代理对象进行交互。

proxy.ts
import { NextRequest, NextResponse, userAgent } from 'next/server'
 
export function proxy(request: NextRequest) {
  const url = request.nextUrl
  const { device } = userAgent(request)
 
  // device.type can be: 'mobile', 'tablet', 'console', 'smarttv',
  // 'wearable', 'embedded', or undefined (for desktop browsers)
  const viewport = device.type || 'desktop'
 
  url.searchParams.set('viewport', viewport)
  return NextResponse.rewrite(url)
}

isBot

一个布尔值,指示请求是否来自已知机器人。

browser

一个对象,包含有关请求中使用的浏览器信息。

  • name: 表示浏览器名称的字符串,如果无法识别则为 undefined
  • version: 表示浏览器版本的字符串,或 undefined

device

一个对象,包含有关请求中使用的设备信息。

  • model: 表示设备型号的字符串,或 undefined
  • type: 表示设备类型的字符串,例如 console, mobile, tablet, smarttv, wearable, embedded,或 undefined
  • vendor: 表示设备供应商的字符串,或 undefined

engine

一个对象,包含有关浏览器引擎的信息。

  • name: 表示引擎名称的字符串。可能的值包括:Amaya, Blink, EdgeHTML, Flow, Gecko, Goanna, iCab, KHTML, Links, Lynx, NetFront, NetSurf, Presto, Tasman, Trident, w3m, WebKitundefined
  • version: 表示引擎版本的字符串,或 undefined

os

一个对象,包含有关操作系统的信息。

  • name: 表示操作系统名称的字符串,或 undefined
  • version: 表示操作系统版本的字符串,或 undefined

cpu

一个对象,包含有关 CPU 架构的信息。

  • architecture: 表示 CPU 架构的字符串。可能的值包括:68k, amd64, arm, arm64, armhf, avr, ia32, ia64, irix, irix64, mips, mips64, pa-risc, ppc, sparc, sparc64undefined