Published on

How to get Request User Agent in Next.js getServerSideProps


Here's a little snippet on how to get request user agent in Next.js's getServerSideProps

export async function getServerSideProps(context) {
  const userAgent = context.req.headers['user-agent'];
  console.log({ userAgent });
  return {
    props: {userAgent}, // will be passed to the page component as props


  • getServerSideProps is used when we need to get some props from the server side.
  • It runs ONLY on the server so you can perform operations like talk to database or perform some operation and then return props to the frontend
  • Then the frontend can use the props in the client side.
  • It has access to all the request related information like host, user agent, path, cookies, etc.
  • With the help of those data, we can dynamically render things. Like for example, context aware download link based on what device you are using

Happy finding user-agent!