Error: During prerendering, fetch() rejects when the prerender is complete.
Unanswered
Molossus of Epirus posted this in #help-forum
Molossus of EpirusOP
Hi everyone. I'm tasked with migrating an old Next.js project to the latest Next.js 16 with Cache Components. Inside one of our dynamic components we have an API call using the native
coming from that API call. The builds passes fine and everything seems to work, but my terminal is full of those logs. I'm suspecting that as the error mentions this is just normal behavior in Next.js during prerendering and probably it's fine to ignore, but I haven't found anything about this in the official docs. Is this the expected behavior?
A minimal example of what I'm doing is:
fetch() wrapped in a try/catch block. In our catch block we do some logic and also log the error with console.error so we kown what went wrong. I'm noticing that running a full production build with next build is showing a lot of logs with "Error: During prerendering, fetch() rejects when the prerender is complete. Typically these errors are handled by React but if you move fetch() to a different context by usingsetTimeout,after, or similar functions you may observe this error and you should handle it in that context."
coming from that API call. The builds passes fine and everything seems to work, but my terminal is full of those logs. I'm suspecting that as the error mentions this is just normal behavior in Next.js during prerendering and probably it's fine to ignore, but I haven't found anything about this in the official docs. Is this the expected behavior?
A minimal example of what I'm doing is:
try {
const res = await fetch("my-api");
// some business stuff
} catch (error) {
//some business stuff
console.error(error) //<- This is getting called during prerender
}5 Replies
@Molossus of Epirus
await connection()I'll see if i can find the docs but if I remmber correctly its trying to prerender the page but cant becuase it requieres run time data using connection insures that theres is a connection and prvents this error
import { connection } from "next/server";
await connection()
try {
const res = await fetch("my-api");
// some business stuff
} catch (error) {
//some business stuff
console.error(error) //<- This is getting called during prerender
}I had the same problem because I was using use cache in a child component after an auth call and it was preventing the pre-render but there was nothing to pre-render because there was an auth call