Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
How to combine multiple getServerSideProps function

I have two pages; index.js and other.js, In the index.js I have a method getServerSideProps;

export async function getServerSideProps(context) 
{
   //code here
}

I want to use this same function in the other.js page. Because the code in index.js getServerSideProps is pretty long, I had to import the getServerSideProps in index.js to other.js, doing something like;

import { getServerSideProps } from "./index";
...
export { getServerSideProps };

It works alright, but the problem is, I want to make another request in the getServerSideProps that only runs in the other.js page. One way of doing this is to copy the getServerSideProps code I wrote for the index.js and paste it in other.js and modify the code. The problem is, like I mentioned earlier, the code is quite huge in the getServerSideProps and I don't want to copy it and paste it in all the pages I need it.

My question is, how do I add another getServerSideProps to the one I already exported from another page. Basically, I want to merge the exported getServerSideProps in index.js to the getServerSideProps I have locally in other.js

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to reuse the same getServerSideProps function, but conditionally run specific code based on which page it's being called from, you can try making use of the context.req object:

index.js

export default function Home({ data }) {
  return <>{data}</>;
}

export async function getServerSideProps(context) {
  let foo;

  // Parse `message.url` into parts. An alternative to using the `URL` class
  // would be to use `context.resolvedUrl`, but that will take more effort to
  // isolate pathnames when query parameters are involved.
  const reqUrl = new URL(
    context.req.url,
    `https://${context.req.headers.host}`
  );
  const thisPage = reqUrl.pathname;
  const queryParams = reqUrl.searchParams;

  switch (thisPage) {
    case "/":
      // code for index.js
      foo = "Hello, index.js!";
      break;

    case "/other":
      // code for other.js
      foo = "Hello, other.js!";
      break;

    default:
      foo = "Hello, world!";
  }

  // common code
  console.log("`thisPage`:", thisPage);
  console.log("`context.resolvedUrl`:", context.resolvedUrl);
  console.log("`queryParams`:", queryParams);

  return {
    props: { data: foo },
  };
}

other.js

import { getServerSideProps } from "./index";

export default function Other({ data }) {
  return <>{data}</>;
}

export { getServerSideProps };
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda