userAgent
userAgent
輔助程式擴展了 Web Request API,並提供額外的屬性和方法,以便與請求中的 user agent 物件互動。
middleware.ts
import { NextRequest, NextResponse, userAgent } from 'next/server'
export function middleware(request: NextRequest) {
const url = request.nextUrl
const { device } = userAgent(request)
const viewport = device.type === 'mobile' ? 'mobile' : '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
這有幫助嗎?