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.