Next.js Discord

Discord Forum

Prevent Server Component from Fetching during Docker Build (App Router)

Answered
English Lop posted this in #help-forum
Open in Discord
Avatar
English LopOP
I have a page.tsx in the new App Router structure that makes a fetch to some endpoint. This endpoint is one of the application's own api routes, and the page.tsx itself is intended to be a Server Component (? - I assume it is by default).

However, when attempting to docker build - I encounter ECONNREFUSED 127.0.0.1:3000. It seems next.js attempts to make a call to the indicated endpoint during the docker build stage.

I get a connection error even when I have a separate local server running on the right port, and also when I change the endpoint to point to a different port (where a local service is running). Is there some port exposing I need to do during the docker build stage, or inside Dockerfile?

In any case, I don't need the app to fetch on build - is there any configuration I can do (e.g. some sort of cache configuration) to indicate there is no need to fetch during build?

Secondarily, is this the right pattern, to have a Server Component page call the Next.JS app's own api route? If it is not, what should be the right pattern?
Answered by European sprat
No, it's anti-pattern for an RSC to call it's own route handler
View full answer

7 Replies

Avatar
European sprat
No, it's anti-pattern for an RSC to call it's own route handler
Answer
Avatar
European sprat
If you make the page dynamic it won't attempt to fetch during build
Avatar
English LopOP
thank you! when you say "make the page dynamic" do you mean to use getStaticProps getServerSideProps - what is the newest way to do this in the new App Router?
i couldn't really see/understand the mapping from Page Router to App Router (although i'm not trying to migrate an application - just starting new) - but it seems Page Router has very clear differentiators between when data is fetched e.g. getStaticProps vs getServerSideProps - but I couldn't understand an equivalent for App Router
Avatar
European sprat
Route segment config option

I think it's export const dynamic = "force-dynamic"
Avatar
English LopOP
thank you! this works
Avatar
Short-eared Owl
I have the same issue described here, except I don't want to use force-dynamic, as I want to cache the response later and use on demand isr with a revalidate path.

if I understand correctly, force-dynamic will disable caching and cause it to fetch on every request that a user makes?

to summarize; my issue is that when next build is ran during the Docker container's build process, it cannot access other docker containers and in this case, that is where my cms is. It's a catch-22 for me so far; I cannot access the cms container during build process, so next cannot be built due to fetch failing and I cannot make my cms available by domain, as nginx can't start until the next container is up.

Feels like this should be a pretty common issue that people have? surprised there doesn't seem to be more people talking about it