Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
Get the hostname inside getStaticProps

Is it possible to get the hostname inside getStaticProps?

export async function getStaticProps(context) {
  // get the hostname 
  const hostname = ?????
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

It's not possible to get the hostname inside getStaticProps as the props generated there are static and do not contain information on the request in any way. To get the hostname, you will have to use getServerSideProps or get the hostname on the client side in the page's component. Here is an example of getting the hostname in getServerSideProps:

export const getServerSideProps: GetServerSideProps<{ host: string; }> = async (context) => {
  return {
    props: { host: context.req.headers.host || null }
  };
};

Here is an example of getting the hostname on the client side component using Location:

function Home() {
  // window can be undefined when static generated, however it will be updated when the page is hydrated
  const hostname = typeof window !== 'undefined' ? window.location.hostname : '';
}

export default Home;
about 4 years ago · Santiago Trujillo Report

0

create function :

export const hostname = "https://urldomain.com"

and import everywhere

import {hostname} from "./hostname";

export async function getStaticProps(context) {
 // get the hostname 
console.log(hostname)
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!