Issue with API requests being called repeatedly in Next.js project.
38 replies
Last updated: Jan 26, 2022
D
when looking at my API requests its saying 7000 requests per day?
Jan 25, 2022, 4:38 PM
Hey User! If you don't have a ton of traffic to your site, it's likely that something is misconfigured on your frontend and you're calling the API repeatedly unintentionally. Can you share you project ID so I can look it up?
Jan 25, 2022, 4:48 PM
D
cjcfec7t
Jan 25, 2022, 4:51 PM
D
Its my first time using sanity
Jan 25, 2022, 4:51 PM
D
I am calling it from getServerSideProps in Next.js
Jan 25, 2022, 4:52 PM
D
export async function getServerSideProps() { const featuredPost = await client.fetch(` *[_type == "post" && featured == true && "Chronicles" in categories[]->title ] | order(dateTime(publishedAt) desc) [0]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink } `); const podcastPosts = await client.fetch(` *[_type == "post" && "Bomb Talk" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink }` ); const blogPosts = await client.fetch(` *[_type == "post" && "Updates" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink }` ); const newsPosts = await client.fetch(` *[_type == "post" && "Chronicles" in categories[]->title ] | order(dateTime(publishedAt) desc) [0..6]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink }` ); return { props: { featuredPost, podcastPosts, blogPosts, newsPosts } } } export default Media
Jan 25, 2022, 4:52 PM
D
here are my calls
Jan 25, 2022, 4:52 PM
Got it, it does look like your
getServerSidePropsis the culprit. How often are you triggering rebuilds? Do you have preview enabled in the Studio?
Jan 25, 2022, 5:05 PM
D
i can try moving it
Jan 25, 2022, 5:17 PM
D
but the strange this is the site has been in staging and no one has been building it (its hosted in aws)
Jan 25, 2022, 5:17 PM
D
could you thinnk of anything else?
Jan 25, 2022, 5:17 PM
It looks like this query is running a lot:
*[_type == "post"]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink }
Jan 25, 2022, 5:30 PM
It looks like this query is running a lot:
*[_type == "post"]{ title, "name": author->name, "categorySlug": categories[]->slug, "categories": categories[]->title, publishedAt, excerpt, slug, mainImage, featured, externalLink }
Jan 25, 2022, 5:30 PM
D
thats strange
Jan 25, 2022, 5:32 PM
D
i cant find the query it must be an older version
Jan 25, 2022, 5:33 PM
D
is there anyway of finding out where it came from?
Jan 25, 2022, 5:33 PM
D
I think i found it
Jan 25, 2022, 5:34 PM
D
its on the home page
Jan 25, 2022, 5:34 PM
D
how many times is it getting called?
Jan 25, 2022, 5:35 PM
D
lol well thats not good
Jan 25, 2022, 5:41 PM
D
okay ill move it into the component
Jan 25, 2022, 5:41 PM
D
i think the re rendering is eating it up
Jan 25, 2022, 5:41 PM
D
thanks for all the help
Jan 25, 2022, 5:41 PM
D
great thanks
Jan 25, 2022, 7:30 PM
M
If you move the query to
getStaticPropsinstead it won't run on every render. Should save you a ton of requests. keep in mind that it will still run more in development
Jan 25, 2022, 10:06 PM
D
I moved it inside the module for now lets see if that works
Jan 25, 2022, 10:15 PM
D
instead of being rendered at the page level and passed down
Jan 25, 2022, 10:15 PM
D
that should help if not I can use staticprops
Jan 25, 2022, 10:15 PM
M
If it's in the component it's likely to keep running every time. A child will rerender when the parent does. If you moved it from
getServerSidePropsto a
useEffecthook or similar it's only going to get worse
Jan 25, 2022, 10:19 PM
M
getServerSidePropsruns when the page loads. Any side effects in a component will run when the component rerenders. Depending on your structure that can be more often than the server render, but never less
Jan 25, 2022, 10:21 PM
D
Yes I only used it for the posts we pulled so there are no other items we need to run server side
Jan 25, 2022, 10:25 PM
D
so taking it out completely at the home page level should be okay
Jan 25, 2022, 10:26 PM
D
lets see tomorrow 🙂
Jan 25, 2022, 10:26 PM
D
hey everyone thanks for all the help
Jan 26, 2022, 3:15 PM
D
its about 43 requests today which means its all good
Jan 26, 2022, 3:15 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.