Fix docerized nextjs website to use site's domain instead of "localhost" when using next-intl
Answered
Asian swamp eel posted this in #help-forum
Asian swamp eelOP
I am using
I am using beta to have access to localization in server components.
next-intl 3.0.0-beta.9 and set defaultLocale: "en" and localePrefix: "always" in createMiddleware. When I navigate to localhost:3000/ it changes it to localhost:3000/en as expected, but when I host the site, so I have domain.com/ it also changes it to localhost:3000/en. If I directly navigate to domain.com/en and any subdomain or another locale, then it works correctly. Do I need to set something, or is it a bug of the beta version ? I am using beta to have access to localization in server components.
Answered by Asian swamp eel
For anyone interested I finally figured it out. Why it happens is well explained here https://github.com/vercel/next.js/issues/37536 There is also a solution that is good and should also work with similar situations.
In case of
In case of
next-intl that solution may not always work. To fix it you must set domains on createMiddleware with [{
domain: "0.0.0.0",
defaultLocale: <your default locale>,
locales: [<all your locales>]
}] This will make next-intl use the actual domain and not localhost. Although I am not sure if it's safe or it has any negatives. I hope it helps someone.4 Replies
Asian swamp eelOP
I think I discovered why it happens. I ran the website in docker container in google cloud run. It has
ENV PORT 3000 and ENV HOSTNAME localhost so it servers node server on url http://localhost:3000 instead of actual domain of the service. I am new to docker so I am not sure how to fix this.Asian swamp eelOP
For anyone interested I finally figured it out. Why it happens is well explained here https://github.com/vercel/next.js/issues/37536 There is also a solution that is good and should also work with similar situations.
In case of
In case of
next-intl that solution may not always work. To fix it you must set domains on createMiddleware with [{
domain: "0.0.0.0",
defaultLocale: <your default locale>,
locales: [<all your locales>]
}] This will make next-intl use the actual domain and not localhost. Although I am not sure if it's safe or it has any negatives. I hope it helps someone.Answer
Broad-snouted Caiman
@Asian swamp eel was this happening with the next
output: standalone option in Docker? I’m running into this issue myself, but it’s not very clear to me what the solution is. I’m still pretty new to Docker and Dockerizing Next.American black bear
@Asian swamp eel Same issue here as well,
next-intl beta 3.0.0 beta and docker standalone