-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting undefined #7
Comments
I'm getting the same error even when I wrap in this.$nextTick() |
How are you injecting your environment variables? |
Mine are undefined in spa mode |
If this is still occuring can someone use code sandbox to recreate this, so I can debug? |
I think @aarcoraci means, in nuxt.config.js
and with the output of i am confused at first, and realize the key means key of the actual
|
Currently this module does not support SPA mode, as for that you can just use the normal I'm working on support for this but I'm not quite sure what it, any ideas would be appreciated. |
Idea for SPA mode (if possible with current nuxt):
|
There is a related discussion about making runtime env vars supported out of the box: nuxt/nuxt#5100. |
@samtgarson Maybe you should alter the nuxt server EDIT: Tested with a serverMiddleware in // nuxt.config.js
export default {
mode: 'spa',
// ...
serverMiddleware: [
function(req, res, next) {
// Authorized env variables to be sent to the client
const allowed = ['BACKEND_URL', 'BACKEND_PORT']
// Filter process.env with allowed env variables
const filtered = Object.keys(process.env)
.filter(key => allowed.includes(key))
.reduce((obj, key) => {
obj[key] = process.env[key]
return obj
}, {})
// Set the ENV cookie with JSON encoded variables
res.setHeader('Set-Cookie', [`ENV=${JSON.stringify(filtered)}`])
next()
}
],
// ...
} Maybe a track to explore... |
That is an interesting one—I also recently saw this implementation which uses the head metadata to store the public env... (unfortunately this repo also includes "shims" for other libraries which I strongly disagree with 😞 ) Not sure which personally I prefer as both are using mechanisms which are not designed for this, but if I had to chose I think I'd chose the head meta route... I always shy away from setting cookies where possible 🍪 Don't have any time right now to work on this but would definitely look at PRs! |
OK got your point, I agree but if you exclude the hacky option (meta/cookie), I guess the only "proper" option requires some async stuff:
If this kind of option sounds ok for you, I'll try to make a PR. |
Yeah, that is an alternative but of course its an extra HTTP request so has performance/network implications, plus seems more fragile to your point about dealing with async-ness. I'd be happy with either of the meta/cookie options right now 👍 |
Really glad I found this conversation. Hard to believe Nuxt still has this limitation. Big problem for projects that use CI/CD build servers. |
Useful thread, thanks. Are there plans to support a 12 factor style approach for env vars in SPA mode from the nuxt core team or would it need to come through this project? Cheers. |
There is a very easy trick that works with SPA mode: app.html <!DOCTYPE html>
<html {{ HTML_ATTRS }}>
<head {{ HEAD_ATTRS }}>
{{ HEAD }}
</head>
<body {{ BODY_ATTRS }}>
<script>window.ENV = {{ ENV_INJECT }}</script>
{{ APP }}
</body>
</html> add this to your nuxt.config.js: const { map, fromPairs, defaultTo } = require('ramda')
{
hooks: {
'vue-renderer:spa:templateParams': meta => {
const injectVariablesWithDefaults = [
['INITIAL_SETUP', false],
]
const processEnvDefault = ([key, defaultValue]) => [key, defaultTo(defaultValue, process.env[key])]
const injectVariables = fromPairs(map(processEnvDefault, injectVariablesWithDefaults))
meta.ENV_INJECT = JSON.stringify(injectVariables)
}
},
} This picks the desired environment variables from Surely there is a way to also append to the other meta variables like Also: you can write this without ramda. I just found it to be easier for me. Best, |
This comment has been minimized.
This comment has been minimized.
@coderua that is a different issue, please don't hijack the thread—the error is saying that This suggests to me your store is not being setup like a normal store. Could you take another look as to why that might be, it's not really within the scope of this library to fix but if you're still stuck please open another ticket with an expanded snippet of your store and I might be able to help. |
Hello,
I'm trying to get this module working but no luck. I have followed the steps stated:
The text was updated successfully, but these errors were encountered: