question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Axios fetcher(METHOD POST) + NextJs SSR = Infinite Endless API calls

See original GitHub issue

Using Method POST in Axios fetcher results in Endless API calls

Description / Observed Behavior

Using Method POST in Axios fetcher results in Endless API calls.

When using fetcher like

const fetcher = (url, data) => axios.post(url, data).then(res=>res.data)
const {data, error} = useSWR(['/api/v1/reports/statistics', data], fetcher)

Endless API calls. Infinite calling

But this happens only in SSR. And works fine with client Side rending.

Expected Behavior

Make only one API call during the server side rendering

Repro Steps / Code Example


const fetcher = (url, data) => axios.post(url, data).then(res=>res.data)

const StatisticsTable = ({criteria={}}) => {

    const {data, error} = useSWR(['/api/v1/reports/statistics', criteria], fetcher)

return <div>{data}</div>
}

Additional Context

SWR version - 0.3.3 to 0.3.5 apiCall

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
promer94commented, Oct 27, 2020
// Don’t do this! Deps will be changed on every render.
useSWR(['/api/user', { id }], query)
// Instead, you should only pass “stable” values.
useSWR(['/api/user', id], (url, id) => query(url, { id }))

https://swr.vercel.app/docs/arguments#passing-objects

Hope this will help

3reactions
huozhicommented, Oct 27, 2020

@vishnuvchandar I tried on your codesanbod example, the issue is when you pass an object to useSWR as first argument within next.js, each time the hashed key will be different

image

the weakmap in hash lib looks like following image

the solution to this could be

+const payload = { test: "message" }
export default function DayPage() {
-  const { data, error } = useSWR(["/api/v1/day", { test: "message" }], fetcher);
+  const { data, error } = useSWR(["/api/v1/day", payload], fetcher);
  return <div>Hello Day {data}</div>;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch data from Next.js API and create an infinite loading list ...
Check out https://screencasts.alterclass.io/ for more episodes!▻ Learn to build your own production-ready React applications and become a ...
Read more >
Data Fetching: getServerSideProps - Next.js
Take the following example. An API route is used to fetch some data from a CMS. That API route is then called directly...
Read more >
Getting infinite loop when making an axios get request
You need to put empty array (dependency) after the callback function: Change to: useEffect(() => { country = getCookie("title"); ...
Read more >
Infinite Scroll React Example with TypeScript and NextJS
For the uninitiated, infinite scroll (otherwise known as endless scroll) refers to a method of automatically loading data when a user ...
Read more >
Data fetching in Next.js — How To Use SWR - Ibrahima Ndaw
The fetcher function enables us to send the HTTP request to the server and then parse the response data to JSON. The fetch...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found