Next.js Discord

Discord Forum

Error occurred prerendering page "/".

Unanswered
Long-billed Curlew posted this in #help-forum
Open in Discord
Long-billed CurlewOP
project is using Nextjs app router (V14.0)
- fetching data from the endpoint (which works fine, already tested),
- passing it to a client component SearchFilter

code:
import SearchFilter from '@/components/primitives/SearchFilter';
import { Books } from '@prisma/client';

export const dynamic = 'force-dynamic';

const fetchData = async (): Promise<any> => {
    const response = await fetch(`${process.env.NEXTAUTH_URL}/api/books`, {
        next: { tags: ['books'] },
        cache: 'no-store',
    });
    const data = await response.json();
    const contents = data.data;
    console.log(`data from '/books' => `, contents.length);

    return contents;
};

export default async function Home() {
    const contents = await fetchData();

    return (
        <section className=" container mx-auto py-6">
            <SearchFilter books={contents} />
        </section>
    );
}


Error:
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  cause: Error: connect ECONNREFUSED ::1:3000
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1555:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -4078,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 3000
  }
}

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:11576:11)

14 Replies

"fetching data from the endpoint (which works fine, already tested)," => it seems not
where is this API located?
note that you can't call your own API during build-time if that's what you happen to do here
Long-billed CurlewOP
yup i'm kinda fetching the data from my same endpoint
@Eric Burel "fetching data from the endpoint (which works fine, already tested)," => it seems not
Long-billed CurlewOP
so that's what's going wrong?
api is located at /app/api/books/route.ts and the pages i'm fetching data are at /app/page.tsx and /app/books/page.tsx
so how can i make my production build comeup with no errors?
Any suggestions @Eric Burel ?
Or maybe i can fetch the data from a server-only function, something like using prisma ?
You can't call your own api indeed
Because it's not running during build for instance
You can directly reuse the code that is within this API route
Long-billed CurlewOP
okay thankz @Eric Burel
is ther any other way you would do it?