Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
How to use window in getStaticProps?

I want to load a specific configuration (opening hours, company address,…) based on the subdomain. Thus I want to run a single instance of NextJS for different clients (each client has a subdomain).

I try

export async function getStaticProps() {
  const subdomain = /:\/\/([^\/?]+)/.exec(window.location.href)[1].split(".")[0];

  const config = mysqlquery(subdomain);

  return {
    props: {
      config,
    }
  }   
}

and I get window is not defined.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

First install absoluteUrl then you should request to your local api to find subdomain let see the code:

export async function getStaticProps() {
   const req = await 
   fetch("http://localhost:3000/api/hello");
  const dd = await req.json();
   console.log("sub", dd);
     return {
     props: {
    aboutData: [],
      },
   };

}

now in pages/api/hello.js

import absoluteUrl from "next-absolute-url";
     export default async function 
            handler(req,res) {
  const { origin } = absoluteUrl(req);
  let subdomain = origin.match(/\w+/);
  console.log("request ", subdomain);
  return res.status(200).json({ subDomain: 
     subdomain });
    }

it will send an array subdomain is the first index of array actually i am not sure about regex that i write you can check if not work write your own regex to determin the subdomain

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can access the url using context.req.headers.referer, being your code as follows:

export async function getStaticProps(context) {

const subdomain = /:\/\/([^\/?]+)/.exec(context.req.headers.referer).split(".")[0];

const config = mysqlquery(subdomain);

return {
    props: {
      config,
    }
  }   
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda