Replies: 8 comments 15 replies
-
I have some next.js libs that might help in this scenario. They haven't been battle-tested yet but I'd love to get them there. |
Beta Was this translation helpful? Give feedback.
-
Also coming across this issue with Contentful's API rate limit of 7 requests per second. |
Beta Was this translation helpful? Give feedback.
-
I was facing a similar issue where doing a static build of 200+ pages that used the GitHub API would hit some secondary rate limits. I resolved this issue by simply adding a 500ms delay before making API calls during the build process. The export function avoidRateLimit(delay = 500) {
if (!process.env.IS_BUILD) {
return
}
return new Promise((resolve) => {
setTimeout(resolve, delay)
})
} So anywhere I made API calls, I could just add it as the first line: async function getReadme() {
await avoidRateLimit()
...
}
async function getRepoDetails() {
await avoidRateLimit()
...
} And in your "build": "IS_BUILD=true next build" And this let the builds go through. Not very elegant but it works |
Beta Was this translation helpful? Give feedback.
-
Thank you for your idea. This is a little more dynamic:
To properly fix that issue, we should catch 429 error and only sleep after that, then repeat the request. |
Beta Was this translation helpful? Give feedback.
-
Hi all, There is a little know NextJS configuration that controls multithreading at build time. I found no mention of it in NextJS's documentation and only stumbled across it perusing the source code. Without any configuration, the NextJS build will attempt to do some sort of multithreading, which results in multiple simultaneous HTTP requests at build time. I don't know what the default configuration is here and how it depends on the build server. In my case, we were using Netlify, so I imagine Netlify's builders also had a play in this. Regardless, the following NextJS configuration in your
After finding this in the code, I also found mention of this functionality here, so I will give credit where credit is due: https://docs.uniform.dev/sitecore/deploy/how-tos/how-to-control-nextjs-threads/ |
Beta Was this translation helpful? Give feedback.
-
@konstantinbrazhnik you are a lifesaver! |
Beta Was this translation helpful? Give feedback.
-
This is a quicker way to solve the issue, it adds a delay of 12 seconds between each api call: By default, Next.js sets the NODE_ENV environment variable to "production" when you run the next build command to create a production build. In development mode, NODE_ENV is set to "development". export async function getStaticProps(context){ |
Beta Was this translation helpful? Give feedback.
-
NextJS 15 now has options to help with this: https://nextjs.org/blog/next-15#advanced-static-generation-control-experimental |
Beta Was this translation helpful? Give feedback.
-
Hey folks!
First time Next user here. I'm trying to build an election information site using the Google Civic Info API. I'd love to use static generation, because the API is only updated every couple of months, usually around election time.
I'd like to create a unique, static route for every The API is uses the Open Civic Data Division ID's (OCD-ID) to map every "political geography" within the country (think "Los Angeles County, California or Michigan's 6th Congressional District"). The ID's use a recursive structure such as below:
ocd-division/country:<country_code>(/<type>:<type_id>)*
So here's where I'm running into issues
Because the API can return a single recursive result, I can search single state, say California, and get every division from the largest all the way down to the smallest. I then want to use that single JSON object to make my static paths and static props. I'll use the OCD-ID's as the path and I'll provide the elected officials as the props.
Everything works fine in development, but then when I try to build the site, things quickly break down because the API hits a 100 request per second rate limit and begins returning
429 errors
. This causes a pre-render error, because props that should be available no longer are.It seems that the API is being called on every page build, even though I only want to call the API once and then traverse that object, using only a tiny fragment for each page. Here is the code below:
Here's a common output from
next build
:Everything works fine with server rendering, but it seems like this could be a textbook case for static generation. Can anyone help show me where I've gone wrong?
Please let me know if I can provide any other info. Thanks in advance for all help!
Beta Was this translation helpful? Give feedback.
All reactions