¿Es posible obtener el nombre de host dentro getStaticProps ?
export async function getStaticProps(context) { // get the hostname const hostname = ????? }No es posible obtener el nombre de host dentro de getStaticProps ya que los accesorios generados allí son estáticos y no contienen información sobre la solicitud de ninguna manera. Para obtener el nombre de host, deberá usar getServerSideProps u obtener el nombre de host en el lado del cliente en el componente de la página. Aquí hay un ejemplo de cómo obtener el nombre de host en getServerSideProps :
export const getServerSideProps: GetServerSideProps<{ host: string; }> = async (context) => { return { props: { host: context.req.headers.host || null } }; }; Aquí hay un ejemplo de cómo obtener el nombre de host en el componente del lado del cliente usando 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;crear función:
export const hostname = "https://urldomain.com"e importar en todas partes
import {hostname} from "./hostname"; export async function getStaticProps(context) { // get the hostname console.log(hostname) }