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
:表示设备类型的字符串,如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
、WebKit
或undefined
。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
、sparc64
或undefined