Categories
React Answers

How to get page URL or hostname in Next.js project?

Spread the love

To get page URL or hostname in Next.js project, we can use next-absolute-url.

For instance, we write

import absoluteUrl from "next-absolute-url";
const { origin } = absoluteUrl(req);
const apiURL = `${origin}/api/job.js`;

to call absoluteUrl with req and then get the hostname from the origin property of the returned object.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *