Skip to main content

Removed parsed User Agent from Middleware API

Why This Error Occurred​

Your application is interacting with req.ua which has been deprecated.

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

export function middleware(request: NextRequest) {
const viewport = request.ua.device.type === 'mobile' ? 'mobile' : 'desktop'

request.nextUrl.searchParams.set('viewport', viewport)
return NextResponse.rewrites(request.nextUrl)
}

Possible Ways to Fix It​

The internal logic has been moved into a separate userAgent function that you can import from next/server and wrap your request instead.

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

export function middleware(request: NextRequest) {
const { device } = userAgent(request)
const viewport = device.type === 'mobile' ? 'mobile' : 'desktop'

request.nextUrl.searchParams.set('viewport', viewport)
return NextResponse.rewrite(request.nextUrl)
}