nextjs15 prerender-error TypeError: fetch failed #83220
-
SummaryWhen building the application, I encounter an error in a server component because the API used by the application is unavailable. I can work around this by adding export const dynamic = 'force-dynamic'; to the page that performs the fetch, but this feels more like a workaround than a proper solution. See the repo for an example. How should this be done? Additional informationNo response Example |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The Error: |
Beta Was this translation helpful? Give feedback.
-
As long as that server is not your own server... cuz otherwise this is an extra HTTP round trip back to yourself - sort of - so it is bound to have some latency. Rather you should go straight to the source. https://nextjs-faq.com/fetch-api-in-rsc. But of course, that source might not be live during build time. You could do this, with the connection API, essentially the same as you did with force-dynamic, but more semantic - you put the limit. When PPR becomes stable, this can also mean, pre-render until this point, and let the rest be calculated at request time. As of today if there data is not there, during build time you have to "defer" it to later on. import { connection } from 'next/server'
import { getPosts } from '@/actions/apiCalls';
import styles from './page.module.css';
async function Posts() {
await connection()
const posts = await getPosts();
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
)
}
export default function Home() {
return (
<div className={styles.page}>
<main className={styles.main}>
<Suspense fallback={'...'}>
<Posts />
</Suspense>
</main>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
As long as that server is not your own server... cuz otherwise this is an extra HTTP round trip back to yourself - sort of - so it is bound to have some latency. Rather you should go straight to the source. https://nextjs-faq.com/fetch-api-in-rsc.
But of course, that source might not be live during build time. You could do this, with the connection API, essentially the same as you did with force-dynamic, but more semantic - you put the limit.
When PPR becomes stable, this can also mean, pre-render until this point, and let the rest be calculated at request time.
As of today if there data is not there, during build time you have to "defer" it to later on.