Next.js · Junior · Question 72 of 75

How does a server component read a request header like user-agent?

import { headers } from 'next/headers'

const ua = (await headers()).get('user-agent')

Explanation

headers() is async in Next.js 16 and returns read-only headers. Using it makes the route render at request time.

Further reading

Problem with a question

Next.js · Junior · question 72

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz