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

241
Visualizações
Apollo Client useQuery making un-necessary refetches upon Next.js router.push

I'm dealing with an issue in which a query inside a useQuery Apollo Client hook is getting un-necessarily re-run whenever Next.js's router.push (docs here) is called.

The abbreviated code is:

const Parent = () => {
  useQuery(QUERY_HERE, {
    onCompleted: () => {
      console.log("just completed apollo query");
    }
  });

  return <Child />;
}

const Child = () => {
  const router = useRouter();
  const currentArg = router.query?.currentArg;

  return (
    <div>
      <button
        onClick={() => {
          if (currentArg === "on") {
            router.push("/?currentArg=off");
          } else {
            router.push("/?currentArg=on");
          }
        }}
      >
        Click me!
      </button>
    </div>
  );
};

You can see a reproduction on CodeSandbox here.

My expectation is that whenever the button is clicked (and thus router.push is called), the query shouldn't be re-run. However, it's re-run any time the button is clicked. This is surprising because my understanding is that an Apollo query should only be re-run when one of its variables change.

Does anyone know of a way to ensure that the Apollo query is not re-run?

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

0

Next.js re-renders pages when navigation occurs and in your example you recreate Apollo Client every time it happens. Then your useQuery component renders again, checks if cache contains data for given query (it doesn't as new client is empty) and sends query again.

To fix this, create your ApolloClient in global scope. This way it'll stay stable and can be reused between multiple queries and renders.

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