Runtime Config on App Router for using different URLs for build and runtime
Answered
Pyrrhuloxia posted this in #help-forum
PyrrhuloxiaOP
As the title suggests, I need to use different configuration in build and runtime.
I have Strapi and NextJs in a Docker Compose, NextJs SSG pages (I don't what's equivalent for that in App Router) is heavily dependent on Strapi service.
So in order to get NextJs app built, I can write the service hostname of Strapi like "http://strapi:1337" in NextJs config and it builds the app perfectly. But when I open the app on browser normally it can't find the URL "http://strapi:1337" because it exist in Docker network not on my local computer. So I need to use "http://localhost:1337" to get my browser be able to reacgh Strapi service but as you can guess, "http://localhost:1337" is not available from NextJs container's localhost.
So if I use "http://localhost:1337" I can't get app built, but if I use "http://strapi:1337", I can get app built but this time, that service is unavialable on browser.
To solve this issue I'm thinking to use separate configuration for build and runtime phases.
But I couldn't find a way to define an environment variable or any configuration that differs from build and runtime.
Is anybody experienced something like this can help me ?
I have Strapi and NextJs in a Docker Compose, NextJs SSG pages (I don't what's equivalent for that in App Router) is heavily dependent on Strapi service.
So in order to get NextJs app built, I can write the service hostname of Strapi like "http://strapi:1337" in NextJs config and it builds the app perfectly. But when I open the app on browser normally it can't find the URL "http://strapi:1337" because it exist in Docker network not on my local computer. So I need to use "http://localhost:1337" to get my browser be able to reacgh Strapi service but as you can guess, "http://localhost:1337" is not available from NextJs container's localhost.
So if I use "http://localhost:1337" I can't get app built, but if I use "http://strapi:1337", I can get app built but this time, that service is unavialable on browser.
To solve this issue I'm thinking to use separate configuration for build and runtime phases.
But I couldn't find a way to define an environment variable or any configuration that differs from build and runtime.
Is anybody experienced something like this can help me ?
Answered by Ray
does this work?
const STRAPI_URL = process.env.NODE_ENV === 'production' ? 'http://strapi:1337' : 'http://localhost:1337'32 Replies
PyrrhuloxiaOP
hey Ray, i'll try this one
PyrrhuloxiaOP
it did work! but i don't understand the logic behind it, next build uses development env?
PyrrhuloxiaOP
but it doesn't exist on next start
@Pyrrhuloxia but it doesn't exist on next start
it doesn't work?
PyrrhuloxiaOP
nope nope it does very well
i mean NODE_ENV doesn't exist on next start right?
I think it does and it should be
production with next startPyrrhuloxiaOP
but process.env.NODE_ENV if value is production then use something else if not use something else, i'm trying to see how this is differs even on a built app
PyrrhuloxiaOP
i mean node_env env var is exist as production in next build but what changes in next start so it differs and why node_env is not included inside the build bundle
anyway never mind, thank you so much, i was trying to fix this like 3 days
sorry I still don't understand your question 😵
it should be
production with next build or next startPyrrhuloxiaOP
got it thanks 🙂
PyrrhuloxiaOP
can i ask a question here? how can i differentiate between client side and server side code in app router?
it was quite straightfoward in pages router
by default, all the components are server component
if you put
'use client' on top the file then it will become a client componentPyrrhuloxiaOP
i want to understand what makes a page generated in build phase, sorry about ignorance
by default all page are static generated
PyrrhuloxiaOP
i see, so a page have to opted out from static generation
yes
PyrrhuloxiaOP
thanks a lot
one last question: "use client" is commands next build to statically generate it right? or vice-versa?
no, it is a directive to mark the component as client component
PyrrhuloxiaOP
gosh nextjs 14 makes it hard to work with docker compose
why?
PyrrhuloxiaOP
usually the dependent service have to be running in order to nextjs to be able build, and depends_on does not work :/