Next.js Discord

Discord Forum

Fetching in a monorepo with Next.js and API server

Answered
Scottish Fold posted this in #help-forum
Open in Discord
Scottish FoldOP
I have a Turborepo with a Next.js app and an API server. A few pages make fetches to the api server in order to display data. In development this is normally not a problem, but when it comes time to build the application, Next.js fails to build because the API server isn't online to fetch and pre-render the pages. Is there a way around this?
Answered by Scottish Fold
I know I can do export const dynamic = "force-dynamic but i'd like to be able to pre render if possible
View full answer

52 Replies

Scottish FoldOP
I know I can do export const dynamic = "force-dynamic but i'd like to be able to pre render if possible
Answer
Scottish FoldOP
web:build: TypeError: fetch failed
web:build:     at node:internal/deps/undici/undici:13178:13
web:build:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
web:build:     at async s (/Users/.../Desktop/Projects/v5-rewrite/apps/web/.next/server/chunks/292.js:1:10356)
web:build:     at async o (/Users/.../Desktop/Projects/v5-rewrite/apps/web/.next/server/app/games/page.js:1:2260) {
web:build:   digest: '2538168642',
web:build:   [cause]: AggregateError [ECONNREFUSED]: 
web:build:       at internalConnectMultiple (node:net:1118:18)
web:build:       at afterConnectMultiple (node:net:1685:7)
web:build:       at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
web:build:     code: 'ECONNREFUSED',
web:build:     [errors]: [ [Error], [Error] ]
web:build:   }
web:build: }
web:build: 
web:build: Error occurred prerendering page "/games". Read more: https://nextjs.org/docs/messages/prerender-error
web:build: 
web:build: TypeError: fetch failed
web:build:     at node:internal/deps/undici/undici:13178:13
web:build:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
web:build:     at async s (/Users/.../Desktop/Projects/v5-rewrite/apps/web/.next/server/chunks/292.js:1:10356)
web:build:     at async o (/Users/.../Desktop/Projects/v5-rewrite/apps/web/.next/server/app/games/page.js:1:2260)
@necm1 it shouldn't stop it to run the build successfully
Scottish FoldOP
sorry I don't understand what you're saying
{
  "$schema": "https://turbo.build/schema.json",
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": [".next/**", "!.next/cache/**"]
    },
    "check-types": {
      "dependsOn": ["^check-types"]
    },
    "dev": {
      "persistent": true,
      "cache": false
    },
    "start": {
      "persistent": true
    },
    "//#check": {},
    "//#check:fix": {
      "cache": false
    }
  }
}
this is my turbo config btw
@Scottish Fold sorry I don't understand what you're saying
nah, my bad. the error shouldn’t prevent your build process
Scottish FoldOP
well
turbo doesn't keep the api server online during build, right
did you try to run next start
Scottish FoldOP
I can't start it if its not built right
mhm, I also had these errors, but it still builded all files
just give it a run
Scottish FoldOP
ok let me try
looks like it’s really preventing to build the static pages
Scottish FoldOP
I mean this is primarily a turborepo question, do you know of a better place to ask those?
thats what I said
Scottish FoldOP
ok
better places could also be the official github repository or stackoverflow
Scottish FoldOP
Okay
could you maybe start your api in dev mode meanwhile it’s building
or are you using next internal api routes
Scottish FoldOP
i'll probably just use force-dynamic
@necm1 or are you using next internal api routes
Scottish FoldOP
no its a separate api server in the monorepo with elysia
@Scottish Fold no its a separate api server in the monorepo with elysia
yea, then maybe start this one before and just run the build
it should work then ig
Scottish FoldOP
yeah that's worked
I was just primarily wondering if turborepo had a thing where you could have a task online while another is running, then exit the first one when the second finishes
dunno if this behavior is related to turbo or your actual usage of your api
had these errors too, but still builded all files
Scottish FoldOP
weird
its just the fact that the api is in a monorepo with the nextjs app
Scottish FoldOP
I know I could have build depend on a startApi task, but it won't finish because startApi runs forever
@necm1 https://turbo.build/repo/docs/crafting-your-repository/running-tasks
Scottish FoldOP
i'll look at this
guess that’s what you need
@Scottish Fold I know I could have `build` depend on a startApi task, but it won't finish because `startApi` runs forever
yea, you probably need to run a subcommand while you try to build your files
may I ask you, why “force-dynamic” wasn’t a option in the first place?
Scottish FoldOP
It was, I was just wondering about other options
oh, my bad
it’s 11pm here 🥲
Original message was deleted
btw. @Scottish Fold could you mark any of the answers here as described here: