0

I have e-commerce nextjs app which have dynamic route pages for cafe menu categories , the menu categories is fetch from backend (django) it's work perfectly when run it in dev

`npm run dev`

but the problem is I get error every time I build next js .

npm run build 

the error:

Error occurred prerendering page "/category/bakery". Read more: https://nextjs.org/docs/messages/prerender-error FetchError: request to http://127.0.0.1:8000/menu/api/category/bakery/ failed, reason: connect ECONNREFUSED 127.0.0.1:8000

the structure of next:

  • pages
    • category * [slug].tsx

slug page :

function Home({ posts, categories }) {
   const router = useRouter();
  

    if (router.isFallback) {
     return <div>Loading...</div>;
       }

     return (
     .
     .

     .
     .
             )
        }

  export async function getStaticPaths() {
    return {
      paths: [{ params: { slug: "dessert" } }],
      fallback: true,
   };
  }

  export async function getStaticProps({ params }) {
     const res = await fetch(`http://127.0.0.1:8000/menu/api/category/${params.slug}/`);
const posts = await res.json();

const ress = await fetch("http://127.0.0.1:8000/menu/api/category/");
const categories = await ress.json();

return {
  props: {
    posts,
     categories,
     },
  };
 }


export default Home

I don't know why this happen I know this question look like duplicate but when I read the previous questions they have different error massage or the question not been solved .

I really appreciate any help I struggle with it for days .

alina
  • 11
  • 6
  • Does this answer your question? [Fetch error when building Next.js static website in production](https://stackoverflow.com/questions/66202840/fetch-error-when-building-next-js-static-website-in-production) – Yilmaz Feb 20 '22 at 21:32
  • No it dose not . – alina Feb 20 '22 at 21:36
  • 1
    Make sure your backend server is running when building the Next.js app. – juliomalves Feb 20 '22 at 23:43
  • 1
    @juliomalves OMG I feel so stupid now thank you problem solved . But excuse me, the same error also appears when running the Docker Compose, knowing that the back-end is being built first.Why hasn't it been solved like when they were run without the Docker Composer? . Docker compose came from nowhere forgive me :) . – alina Feb 21 '22 at 13:56

0 Answers0