getServerSideProps access current browser url

Viewed 4899

I am calling getServerSideProps and passing in the req and res parameters like this:

export async function getServerSideProps({ req, res }) {}

I need to get the current browser url path and I can't find it in the request object. Is there a way to get the current url inside getServerSideProps?

1 Answers

You can use the resolvedUrl field from the context parameter.

export async function getServerSideProps({ req, res, resolvedUrl }) {
    console.log(resolvedUrl)

   // Remaining code
}

From the getServerSideProps documentation:

resolvedUrl: A normalized version of the request URL that strips the _next/data prefix for client transitions and includes original query values.

Note that resolvedUrl will not return the domain part of the URL, only the path and query string are returned.

Related