#Error: During prerendering, fetch() rejects when the prerender is complete.

1 messages · Page 1 of 1 (latest)

keen pecan
#

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 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 using setTimeout, 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
}
idle summitBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

tawny pecanBOT
# keen pecan Hi everyone. I'm tasked with migrating an old Next.js project to the latest Next...
Please add more information to your question

Your question currently does not have sufficient information for people to be able to help. Please add more information to help us help you, for example: relevant code snippets, a reproduction repository, and/or more detailed error messages. See more info on how to ask a good question in https://discord.com/channels/752553802359505017/1138338531983491154 and #welcome message.

keen pecan
#

Error: During prerendering, fetch() rejects when the prerender is complete.

plucky stratus
#

@keen pecan ```
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
}