Next.js Discord

Discord Forum

TypeError: fetch failed only in production build, but works in local

Answered
Bigheaded ant posted this in #help-forum
Open in Discord
Bigheaded antOP
I'm building a site with NextJS that uses Strapi for the content, I'm self hosting both in a Coolify instance and i na VPS. So when developing the site locally I use the public strapi url and works just perfectly, but then into the production build site and after navigating into a page with parameters (which is just the page id to fetch individual data) throws this TypeError: fetch failed and I've been struggling to find the solution
TypeError: fetch failed
    at node:internal/deps/undici/undici:12618:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async p (/app/.next/server/app/proyectos/[slug]/page.js:1:14879) {
  cause: ConnectTimeoutError: Connect Timeout Error
      at onConnectTimeout (node:internal/deps/undici/undici:7760:28)
      at node:internal/deps/undici/undici:7716:50
      at Immediate._onImmediate (node:internal/deps/undici/undici:7748:13)
      at process.processImmediate (node:internal/timers:476:21)
      at process.callbackTrampoline (node:internal/async_hooks:128:17) {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  },
  digest: '302496454'
}

And this is my code that's generating the issue
import { notFound } from "next/navigation";

export default async function Proyectos({params}) {

  const query = await fetch(`${process.env.STRAPI_URL}/api/proyectos?filters[slug][$eq]=${params.slug}&populate=*`);

  const response = await query.json();
  const proyecto = response.data[0];
  if(!query.ok||!proyecto){
    notFound()
  }

  return (<>
  {proyecto.name}  
  </>
)

}

I've already verified that the .env variable is properly set, that the endpoint has no cors issues and that the "params" is not null. What weirdens me the most is that I have another call to that same api (another route, just /api/proyectos) works just fine, and that call is at app/page.tsx
What else should I try? thanks in advance!
Answered by Bigheaded ant
the problem was that if you are using dynamic routing as I did (like app/projects/[slug]/page.tsx) you need to use generateStaticParams so next can generate all the routes in build time
View full answer

9 Replies

Bigheaded antOP
any suggestions? i forgot to mention I'm using next 14.2
Horse guard wasp
cause: ConnectTimeoutError: Connect Timeout Error
sounds like ur request cant reach the strapi api, waits for a while until the connection times out, check config? url?
Horse guard wasp
possibly check if this "${process.env.STRAPI_URL}" is correct or not, maybe its not getting the right env variable
im also kind of a beginner btw so if someone else has an idea feel free to chime in
Horse guard wasp
what ended up being the problem
Bigheaded antOP
the problem was that if you are using dynamic routing as I did (like app/projects/[slug]/page.tsx) you need to use generateStaticParams so next can generate all the routes in build time
Answer
Bigheaded antOP
this means you have to call an endpoint with all the possible slugs, and then in the component call again but only for the slug you need
honestly it was very difficult to come to that conclusion 😅 thanks for asking btw!