Can we use API_URL and API_URL_BROWSER for nuxt start instead of nuxt build?

damienbeaufils
5
damienbeaufils
commented 2 years ago

Hello,

In my project, we use Nuxt and the Axios module, and we have different environments (local, staging, prod…). We would like to build once the application, and deploy it everywhere, on any environment.
Today we cannot build only once because we have different API_URL and API_URL_BROWSER values, depending on the environment, and necessary for the nuxt build phase.
So, on each server, we have to build and start, instead of just starting the same application everywhere.
For example, today we have to do:
API_URL=http://staging.api.url/ API_URL_BROWSER=http//staging.api.url.browser/ npm run build && npm start on staging environment, and API_URL=http://prod.api.url/ API_URL_BROWSER=http//prod.api.url.browser/ npm run build && npm start on prod environment.

Is there a way to build the application only once, and use the API_URL and API_URL_BROWSER variables when starting the application?
For example, we would like to do:
npm run build only once, without any variable, and just API_URL=http://staging.api.url/ API_URL_BROWSER=http//staging.api.url.browser npm start on staging environment, and API_URL=http://prod.api.url/ API_URL_BROWSER=http//prod.api.url.browser npm start on prod environment.

I saw the Dynamic API Backend documentation, which use the @nuxtjs/proxy module, but I was wondering if there is another way.

Thanks!

0
pi0
37.3k
pi0
commented 2 years ago

Hi. The best option for multi-stage projects is usinging a Reverse Proxy (Like nginx or proxy module). We have exactly same pipeline as you mentioned at our company and use proxy. If there is any concern/limitation with this approach will be happy to help.

0
IGassmann
0
IGassmann
commented 2 years ago

@pi0 Why is a reverse proxy the best option for this problem? I'm not sure to understand the necessity of it.

0
Fight-for-Freedom2019
0
Fight-for-Freedom2019
commented 2 months ago

you can npm install @nuxtjs/dotenv and use APIAPIBROWSER and API_URL in .env files made in the root dir

0
Informations
QuestionUnresolved
#c47 - Created 2 years ago