Skip to main content

Removed req.ua from Middleware API

Why This Error Occurred​

Your middleware is interacting with req.ua and this feature needs to opt-in.

import { NextRequest, NextResponse } from 'next/server'

export function middleware(request: NextRequest) {
const url = request.nextUrl
const viewport = request.ua.device.type === 'mobile' ? 'mobile' : 'desktop'
url.searchParams.set('viewport', viewport)
return NextResponse.rewrite(url)
}

Possible Ways to Fix It​

To parse the user agent, import userAgent function from next/server and give it your request:

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)
}