👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Tips for reducing API requests and bandwidth usage on a high-traffic Next.js blog

8 replies
Last updated: Mar 23, 2022
Hi, I have a next.js blog that receives a lot of traffic. Currently, my API Requests and Bandwidth usage are 30x more than what's included....with very expensive overage costs 😞 Is there anything I can do to reduce this usage? Any guides to refer to I'm not finding? I won't be able to afford this long term. Thank you
Mar 23, 2022, 2:15 AM
Are you using getStaticProps for fetching blog data or getServerSideProps?
Mar 23, 2022, 3:38 AM
GetStaticProps
Mar 23, 2022, 3:40 AM
Do you host any videos or high-resolution images on your blog? Also, DM me your project ID!
Mar 23, 2022, 3:45 AM
GetStaticProps
Mar 23, 2022, 3:40 AM
My requests and bandwidth were also quite more than expected; in my case it had to do with non-human bots visiting the site, which triggered the code of the page just like a regular visit, which means my queries ran.
I was in a position where I could safely keep them from visiting without adversely affecting the site. If you have or can create/track some sort of log mechanism, pay attention to where the visits are coming from and, if possible, the user agents. There may be an opportunity to trim the fat ( just make sure you still allow responsible/moderate and probably-necessary bots like Google to visit )

We were headed for 25/mo between bandwidth and requests and now it's probably going to be 2 to 4.
Mar 23, 2022, 5:23 AM
user S
Thank you for the tip. Will look into bots -- had a logger running on my Vercel instance for the past week. Doing a deep dive today. How did you engineer the bot wall?
Mar 23, 2022, 1:36 PM
user B
We are actually using the PHP version of the Sanity client and on and old-timey Apache site, but one with a weird config and that we couldn't use .htaccess or robots.txt for; the client was imported with the require function into the few relevant pages that needed to pull in Sanity content, so I used PHP to look at the user agent string and wrapped the client's "fetch" command in a condition that only let the code inside get run if it it didn't match one of a few different kinds of substrings ( like "petal" for PetalBot, for example). They still visited but they stopped triggering my Sanity code.
In the case of the particular bots we targeted, like an uptime bot, no issue arrises from that content missing from the page, so it was okay to proceed with that approach.

If I ever do something Next-y or
jamstack -y, I might try the FingerprintJS middleware
Mar 23, 2022, 2:08 PM
Wow cool. Super helpful, thank you!
Mar 23, 2022, 4:05 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?