-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Exposing configuration to the server / client side (minor) #3882
Conversation
server/index.js
Outdated
|
||
// Only the `public` key is exposed to the client side | ||
// It'll be rendered as part of __NEXT_DATA__ on the client side | ||
if (this.nextConfig.config.public) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timneutkens
May be this will allow to write code easily.
this.renderOpts.config = {
public: this.nextConfig.config.public || {}
}
So, someone can write this without getting any errors:
import config from 'next/config'
const { mySecret } = config.public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added here: 859c38a
Because I don't want to add it to getInitialProps if it's not configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timneutkens hmm. Okay that's not what I wanted. But I feel like this is just fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
After feedback from @rauchg I'm renaming the next.config.js key to |
Has this been merged in to
Code: import getConfig from 'next/config';
const config = getConfig(); next.config.js: module.exports = {
distDir: 'dist',
runtimeConfig: {
public: {
apiURL: process.env.API_URL || 'https://xxxxxx/api/v1'
}
}
}; build command: yarn build:local
yarn run v1.5.1
$ API_URL='http://localhost:8081/foo/api/v1' next build
> Using external babel configuration
> Location: "/Users/jcol53/work/get-help/fe/.babelrc" Running with public: {
test: "boo"
} was no different. It sure looks like the relevant file is included in the local package.json: ✗ cat node_modules/next/package.json| grep -C 3 config
"asset.js",
"error.js",
"constants.js",
"config.js"
],
"bin": {
"next": "./dist/bin/next" |
@jcheroske yes, the config signature was changed though, after this PR was merged: https://github.com/zeit/next.js#exposing-configuration-to-the-server--client-side |
OK cool, that helps. How would I pull in an console.dir('process.env.API_URL', process.env.API_URL);
console.dir('process.env ', process.env);
module.exports = {
distDir: 'dist',
publicRuntimeConfig: {
apiURL: process.env.API_URL || 'https://foo/api/v1'
}
}; It seems like my env vars aren't getting through: ✗ API_URL='http://localhost:8081/foo/api/v1' next build
'process.env.API_URL'
'process.env '
> Using external babel configuration
> Location: "/Users/jcol53/work/get-help/fe/.babelrc"
✨ Done in 12.08s.
But clearly I'm calling them right: ✗ API_URL='foo' node -e 'console.log(process.env.API_URL)'
foo Is there a sub-process involved here? I'm unclear on how |
If you think this should be moved to an issue feel free to say so. |
It's |
Ah, ok cool thanks. |
I've got a similar issue where I want to supply a different API_URL depending on whether the environment is local development, staging or production. My problem though, is that I'm exporting static sites to run on s3. I'd like to be able to run something like
I've managed to get it working using a custom _document.js similar to this example #1488 (comment). However, it feels a bit hacky and something that would be better done in publicRuntimeConfig |
It's because you're not providing the variable to
|
Thanks @timneutkens. I didn't realise you had to define the env variable every time. It now works a lot cleaner |
Fixes #3856
The
config
key allows for exposing runtime configuration in your app. All keys are server only by default. To expose a configuration to both the server and client side you can use thepublic
key.