nuxt-community / dotenv-module

Loads your .env file into your application context
MIT License
495 stars 30 forks source link

'only' option not working as expected: removes variables both from process + context #54

Closed hartmut-co-uk closed 3 years ago

hartmut-co-uk commented 4 years ago

Version

v1.4.0

Reproduction link

https://codesandbox.io/s/awesome-sanderson-vijy1

Steps to reproduce

  1. Visit reproduction link provided
  2. Reload home route '/'
  3. Check server log in Terminal
  4. toggle routes '/about' <> '/'
  5. Check client log in Console

What is expected ?

console.log server Terminal

/home | fetch() | from server
/home | fetch() | process.env.TEST1 TEST1
/home | fetch() | context.env.TEST1 undefined
/home | fetch() | process.env.TEST2 TEST2
/home | fetch() | context.env.TEST2 TEST2
/home | fetch() | process.env.TEST3 undefined
/home | fetch() | context.env.TEST3 undefined

console.log client Console (toggle route '/about' <> '/')

/home | fetch() | from client
/home | fetch() | process.env.TEST1 undefined
/home | fetch() | context.env.TEST1 undefined
/home | fetch() | process.env.TEST2 TEST2
/home | fetch() | context.env.TEST2 TEST2
/home | fetch() | process.env.TEST3 undefined
/home | fetch() | context.env.TEST3 undefined

What is actually happening?

console.log server Terminal

/home | fetch() | from server
/home | fetch() | process.env.TEST1 undefined
/home | fetch() | context.env.TEST1 undefined
/home | fetch() | process.env.TEST2 TEST2
/home | fetch() | context.env.TEST2 TEST2
/home | fetch() | process.env.TEST3 undefined
/home | fetch() | context.env.TEST3 undefined

console.log client Console (toggle route '/about' <> '/')

/home | fetch() | from client
/home | fetch() | process.env.TEST1 undefined
/home | fetch() | context.env.TEST1 undefined
/home | fetch() | process.env.TEST2 TEST2
/home | fetch() | context.env.TEST2 TEST2
/home | fetch() | process.env.TEST3 undefined
/home | fetch() | context.env.TEST3 undefined

Additional comments?

I'd expect to have control over environment variables visibility via 'only' option.

E.g. with .env file

STRIPE_SECRET_KEY="1234"
STRIPE_PUBLISHABLE_KEY="9876"

and configuring ['@nuxtjs/dotenv', { only: ['STRIPE_PUBLISHABLE_KEY'] }] both variables should be available server-side - while client-side only STRIPE_PUBLISHABLE_KEY should be accessible.

This bug report is available on Nuxt community (#c33)
atinux commented 3 years ago

Closing in favour of using the runtimeConfig, here it is better to specify them in privateRuntimeConfig instead of using only.

See https://nuxtjs.org/blog/moving-from-nuxtjs-dotenv-to-runtime-config#migrating-to-the-nuxtjs-runtime-config-from-nuxtjsdotenv