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!