I’m trying to access a request’s User-Agent header from a route handler I have on my next.js project.
Here is an example:
import { userAgent } from "next/server";
export const dynamic = "force-static";
export async function GET(req: Request) {
console.log(userAgent(req))
return Response.json({ error: false, message: "Hello, world!" });
}
The console.log
function returns this:
{
ua: '',
browser: { name: undefined, version: undefined, major: undefined },
engine: { name: undefined, version: undefined },
os: { name: undefined, version: undefined },
device: { vendor: undefined, model: undefined, type: undefined },
cpu: { architecture: undefined },
isBot: false
}
The above code is using the userAgent function, which I know is designed for middleware, but for some reason middleware doesn’t seem to have an effect on Route Handlers.
I’ve also tried:
- the headers function
- Using req.headers.get(“User-Agent”)
and all of the approaches return a null
/empty string value. I know the request doesn’t send a null user-agent because if I send a request to the same hosted project on Vercel and open up the Logs, the request shows a valid User-Agent column.