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

234
Views
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 answers
Answer question

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 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!