👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with API requests being called repeatedly in Next.js project.

38 replies
Last updated: Jan 26, 2022
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
cjcfec7t
Jan 25, 2022, 4:51 PM
Its my first time using sanity
Jan 25, 2022, 4:51 PM
I am calling it from getServerSideProps in Next.js
Jan 25, 2022, 4:52 PM
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
here are my calls
Jan 25, 2022, 4:52 PM
Got it, it does look like your
getServerSideProps
is the culprit. How often are you triggering rebuilds? Do you have preview enabled in the Studio?
Jan 25, 2022, 5:05 PM
i can try moving it
Jan 25, 2022, 5:17 PM
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
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
Do you recognize it, configured to a client on
v2021-11-30
?
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
thats strange
Jan 25, 2022, 5:32 PM
i cant find the query it must be an older version
Jan 25, 2022, 5:33 PM
is there anyway of finding out where it came from?
Jan 25, 2022, 5:33 PM
I think i found it
Jan 25, 2022, 5:34 PM
its on the home page
Jan 25, 2022, 5:34 PM
how many times is it getting called?
Jan 25, 2022, 5:35 PM
Essentially all of what you see in your stats is that.
Jan 25, 2022, 5:35 PM
lol well thats not good
Jan 25, 2022, 5:41 PM
okay ill move it into the component
Jan 25, 2022, 5:41 PM
i think the re rendering is eating it up
Jan 25, 2022, 5:41 PM
thanks for all the help
Jan 25, 2022, 5:41 PM
We’ll check tomorrow to see if the calls have levelled off.
Jan 25, 2022, 5:42 PM
great thanks
Jan 25, 2022, 7:30 PM
If you move the query to
getStaticProps
instead 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
I moved it inside the module for now lets see if that works
Jan 25, 2022, 10:15 PM
instead of being rendered at the page level and passed down
Jan 25, 2022, 10:15 PM
that should help if not I can use staticprops
Jan 25, 2022, 10:15 PM
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
getServerSideProps
to a
useEffect
hook or similar it's only going to get worse
Jan 25, 2022, 10:19 PM
getServerSideProps
runs 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
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
so taking it out completely at the home page level should be okay
Jan 25, 2022, 10:26 PM
lets see tomorrow 🙂
Jan 25, 2022, 10:26 PM
Looks like things have levelled off significantly.
Jan 26, 2022, 12:23 AM
hey everyone thanks for all the help
Jan 26, 2022, 3:15 PM
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.

Was this answer helpful?