Cannot access $DEPLOY_URL with netlify build

Hello,

I am using Nuxt in a new project and deploy it as a static site through the nuxt generate command.
I’m also using the nuxt-http module which requires absolute URLs.

I try to set the baseURL based on environment variables given by Netlify: $URL and $DEPLOY_URL.

My netlify.toml file is the following:

[build]
command = 'nuxt generate'
publish = "./dist"
functions = "./functions"

[context.production]
command = 'BASE_URL=$URL nuxt generate'

[context.deploy-preview]
command = 'BASE_URL=$DEPLOY_URL nuxt generate'

[dev]
command = 'nuxt'
autoLaunch = false

When I run netlify deploy, it skips the build step, which is maybe intentional.

When I run netlify build, the context is always production and never deploy-preview.

How can I pass $DEPLOY_URL to my build step? How to link build and deploy steps?

Besides, the $URL env variable does not start with https whereas a certificate was created in the Netlify admin.

Thanks for your help.

I read the following resources before asking:

We really recommend people NOT use absolute URL’s for exactly reasons like this; use only /, and then the site is portable to run locally or at any URL you might access it at. Does that really not work for nuxt, or just your special config?

Regardless, we can try to help troubleshoot.

Running netlify build locally may not get you the URL you want since of course WE set $URL only in our build env (maybe you set it locally, not sure), but in our CI, does that not work as expected? I have had some spotty luck using a VAR=value command prefix like that; might try instead something like: nuxt generate -DBASE_URL=$URL since that will mean the shell can’t get in the way of getting that value through to your build command. Of course nuxt has to “do the right thing” there - and not sure it has such facilities, but hopefully you know a trick there :slight_smile:

OK, it seems that my real issue comes from using absolute URLs when I don’t need them. Thank you for pointing me to the right direction.

1 Like

Actually, I better understand my mistake. Since I use nuxt-generate to build the static site, there are parts of my code that rely on node-fetch to get content and it of course needs an absolute URL during the generation process. That’s why the logs tell me I need an absolute URL.

As of now, I inject the URL I need in the command as follows:

[context.production]
command = 'BASE_URL=$URL yarn generate'

[context.branch-deploy]
command = 'BASE_URL=$DEPLOY_PRIME_URL yarn generate'

And reuse it in my Nuxt config or the localhost URL in dev mode:

const baseURL = process.env.BASE_URL || 'http://localhost:3000'

export default {
  http: {
    baseURL
  }
}

And even with this config, it means my functions must be published on Netlify so that they are used during the generation process in production mode.

I read the Nuxt team is working on a full static mode and a way to better work with services like Netlify, out of the box. Can’t wait.

1 Like

thanks for sharing this, @lucpotage!

Somewhat related to this… here is my netlify.toml

[context.deploy-preview.environment]
  REACT_APP_REDIRECT_URI = '$DEPLOY_URL'
  REACT_APP_LOGOUT_URL = '$DEPLOY_URL/logout'

This configuration was working and recently stopped working. The DEPLOY_URL variable doesn’t appear to be setting in the environment variables. Instead, the actual template literal “$DEPLOY_URL” is being set as the environment variables now.

Any idea why this is happening? How can I set netlify deploy-url variables as env vars before the build start?

I took a look at this thread and found a solution:

[context.deploy-preview]
  command = "export REACT_APP_REDIRECT_URI=$DEPLOY_URL 
REACT_APP_LOGOUT_URL=$DEPLOY_URL/v2/logout && npm run build"
2 Likes

In case it helps new users, I now install netlify-plugin-contextual-env. It solved all my issues.

Here is part of my .toml file:

[[plugins]]
package = 'netlify-plugin-contextual-env'
  [plugins.inputs]
  mode = 'suffix'

Let’s say I have this var in my deploy settings:

BASE_URL=https://welcome.io

I also add a variant with a suffix corresponding to my next deploy branch:

BASE_URL=https://next.welcome.io

And I have a local .env file with:

BASE_URL=http://localhost:8888

This way, I can have fully qualified URLs in all my contexts.

1 Like

For future readers, this works for me, thanks @bigwoof91