next.js: publicRuntimeConfig undefined in staging environment
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior
Steps to Reproduce (for bugs)
next.config.js
module.exports = (phase, { defaultConfig }) => {
return {
publicRuntimeConfig: {
defaultEnvironmentSettings: 'test',
},
webpack: (config, { buildId, dev, isServer, defaultLoaders }) => {
// XXX https://github.com/evanw/node-source-map-support/issues/155
config.node = {
fs: 'empty',
module: 'empty',
};
return config;
},
};
};
page
const { publicRuntimeConfig } = getConfig();
const { groupSettings, url } = this.props;
const schoolName = url.query.schoolName;
console.log('publicRuntimeConfig', publicRuntimeConfig) // publicRuntimeConfig undefined
console.log('getConfig()', getConfig()) // getConfig() serverRuntimeConfig:{}, publicRuntimeConfig: undefined
Context
publicRuntimeConfig is undefined
This only happens when I use NODE_ENV staging, it works fine in development mode. (I haven’t tested against production env)
Note that my staging environment is hosted on AWS lambda, but it’s maybe not related to the issue.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 15
- Comments: 28 (14 by maintainers)
@wescoder I had the same issue on the test env, but it makes sense because next server doesn’t run during tests. In that case, I simply changed the
getConfig()call to:That nulls out the values, you can set appropriate defaults if you need them during tests, we don’t in our case.
I still cannot repro the issue in this thread. We now have this config running in production - out to ~2 million users, built, exported, deployed to CDN configured using
assetPrefixusing static JS bundles and a custom node server - without issue.If you run into issues where
publicRuntimeConfigis undefined within testcases, you can use the following in a setup file;@robinvdvleuten what worked for me was:
For reference, I was required to mock the
next/configsingleton to get this working in a Jest test environment. It seems to be required due the way Jest manages its ownrequire.cache, but I’m not sure why no one else has run into this in regards tonext/config. Anyways, the below works for me.@joaovieira yes that’s exactly the same as my code 👍
Just another one to this thread. After a long time struggling to understand why my
next/configmanual mock wasn’t being run, I found out that after transpilation (babel-jest) the module is renamed tonext-server/config(re-exported in https://github.com/zeit/next.js/blob/v8.0.3/packages/next/config.js) and thus not the same module I was mocking. I.e.:Changing the mock to
jest.mock('next-server/config')works as expected… This is horrible to diagnose and I believe this worked correctly before, so not sure something changed in eitherbabelorbabel-jestto cause the optimization.I know this closed but to the original issue of having
publicRuntimeConfigundefined in non-dev environments, disabling automatic static optimisation worked for me in this case i.e. overriding_app.jsand uncommenting the getInitialProps function. Might be a temporary fix though.In case it helps others, my solution (derived from @Vadorequest) follows.
I determine which “environment” to pull the runtime info from, based on whether the code runs on the server or browser.
I’m running a custom Express server. Here’s my snippet for creating the
next app–Right after
nextAppis created,getConfig()will return what you expected. Before that point,getConfig()returnsundefined.@joaovieira https://github.com/zeit/next.js/pull/6458, basically what this does is bundle the wrapper module and mark the require for next-server/config etc as external in webpack. That way it achieves the same without doing code modification 👍
@dcalhoun Your answer was very helpful as it solved a similar
publicRuntimeConfigerror I was receiving, so thank you!!@codinronan Yeah it makes sense, I tested it this way and it worked fine. Thanks!
Just deployed a create-next-app with the code from your issue: https://create-next-example-app-nagabdohry.now.sh/ as you can see in the console it’s working fine.