-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
publicRuntimeConfig undefined in staging environment #4024
Comments
Is there any more information I can provide to help solve this issue? It's quite a critical one. |
@Vadorequest I've just tested this case myself and had no issue. I also spent an hour walking through the entire code path in the Next source to see what it might be and the thing is, the loading of those variables is never dependent on the NODE_ENV (or dev, or any other env-specific value). Since we have the same use case, I need to make sure this works. Not sure what to tell you. What version of the canary are you on? The relevant change is in 5.0.1-canary 10. I'm testing with 17. |
@codinronan I'm using I also checked the source code and didn't see anything that may be the source of this bug. Indeed, the NODE_ENV may not be related at all, and in this case it's likely a behaviour related to AWS hosting? Thank you for your feedback! |
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. |
Okay, thanks for your feedbacks. So, the issue is either related to:
I don't see any other thing that may impact this feature. I'll try to investigate each of those. |
The source of the problem is that I don't specify a config when starting my next.js app:
Maybe Next is failing at resolving the config on AWS? |
I changed the file function setConfig(configValue) {
console.log('next:runtime-config:setConfig()', configValue) // added this line
runtimeConfig = configValue;
} When I run
So, I can tell it's not related to the NODE_ENV. When I run When I run With the following scripts in package.json: "scripts": {
"build:hep": "cross-env-shell GROUP_NAME=staging.hep NODE_ENV=staging \"next build && serverless package -s hepStaging\"",
"deploy:hep": "cross-env-shell GROUP_NAME=staging.hep NODE_ENV=staging \"npm run build:hep && serverless deploy -v --package .serverless -s hepStaging\"",
}, I don't get any log about When I go on my website at https://staging.hep.loan-advisor.studylink.fr/ I don't see any log about |
@codinronan @timneutkens I found a proper workaround. The issue is that Next is failing at resolving the If I provide a Here is what I changed to make it work: I created a const serverRuntimeConfig = {
serverRuntimeConfigValue: 'test server'
};
const publicRuntimeConfig = {
GROUP_NAME: process.env.GROUP_NAME,
publicRuntimeConfigValue: 'test public'
};
module.exports = {
serverRuntimeConfig,
publicRuntimeConfig,
}; I changed my // const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
const { serverRuntimeConfig, publicRuntimeConfig } = require('./next.runtimeConfig');
module.exports = (phase, { defaultConfig }) => {
return {
serverRuntimeConfig,
publicRuntimeConfig,
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;
},
};
}; I changed my app.js to force include a ...
import { serverRuntimeConfig, publicRuntimeConfig } from '../../../next.runtimeConfig';
const nextConf = {
serverRuntimeConfig,
publicRuntimeConfig,
};
// XXX next.dev enables HMR, which we don't want if not in development mode, or when we are on AWS's infrastructure
const nextAppConfig = {
dev: !isHostedOnAWS() && process.env.NODE_ENV === 'development',
conf: nextConf,
quiet: false,
};
const nextApp = next(nextAppConfig); With those changes, it works properly on AWS and I get a I don't know what is the cause of the bug.. And I'm just thinking that maybe providing a If that the case, I guess the doc should be updated to make it very clear. Since Next.js auto-resolves the next.config.js on its own, I was expecting it to do it as well when started from Express or another server. |
@Vadorequest what does your build/deploy stack look like? When it is running on AWS, are you using Are you using |
@codinronan Since you're asking, I guess the encountered behaviour isn't the expected behaviour? I am running Next.js on AWS Lambda, in non-dev mode, I basically build the Next app, which is then loaded by an Express server and served by AWS. You can find the repository there: https://github.com/Vadorequest/serverless-with-next The scripts that are of interest are there: https://github.com/Vadorequest/serverless-with-next/blob/master/package.json#L14 (Lines 9 and 14) And the Next/Express startup is there https://github.com/Vadorequest/serverless-with-next/blob/master/src/functions/server/app.js#L12 It's not the actual repository I'm working on, but it's the closer I've got because that's kinda my playground for Next+Express+AWS apps. |
I'm having the same issues, but instead on |
@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
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 |
@codinronan Yeah it makes sense, I tested it this way and it worked fine. Thanks! |
@codinronan I hear you, maybe it's related to the way AWS lambda stores files or something related to their environment, which isn't the traditional server most people use. I really don't know. Anyway, since I found a proper workaround, by loading the config explicitly, it isn't a real issue anymore. Thank you for your help. |
If you run into issues where // jest.setup.js
import { setConfig } from 'next/config'
import config from './next.config'
// Make sure you can use "publicRuntimeConfig" within tests.
setConfig(config.publicRuntimeConfig) |
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
Right after |
@robinvdvleuten what worked for me was: // jest.setup.js
import { setConfig } from 'next/config'
import { publicRuntimeConfig } from './next.config'
// Make sure you can use "publicRuntimeConfig" within tests.
setConfig({ publicRuntimeConfig }) |
@joaovieira yes that's exactly the same as my code 👍 |
For reference, I was required to mock the // jest.setup.js
import mockEnvConfig from '~/env-config.js';
jest.mock('next/config', () => () => ({ publicRuntimeConfig: mockEnvConfig }));
// jest.config.js
module.exports = {
setupFiles: ['<rootDir>/jest.setup.js'],
}; |
@dcalhoun Your answer was very helpful as it solved a similar |
Came here with the same publicRuntimeConfig issue. I'm new to testing and NextJS, so it's been quite a journey figuring out how and what to test (especially since React, Redux, and other dependencies are mixed in). When I replaced my jest.setup.js file with @robinvdvleuten's suggestion, the publicRuntimeConfig issue was resolved: // jest.setup.js
import { setConfig } from 'next/config'
import { publicRuntimeConfig } from './next.config'
// Make sure you can use "publicRuntimeConfig" within tests.
setConfig({ publicRuntimeConfig }) Then my other tests stopped working. I ended up including both Enzyme and Next configurations in my file and now they both work. Here's how I configured it in my jest.setup.js file in case anyone ever finds themselves in my shoes:
Though i wonder if down the line i'll need to use @dcalhoun configuration solution... |
Just another one to this thread. After a long time struggling to understand why my // path.ts
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
const { assetPrefix = '', basePath = '' } = publicRuntimeConfig; // path.test.ts
jest.mock('next/config', () => {
console.log('this is never run!');
return () => ({
publicRuntimeConfig: {
assetPrefix: 'https://example.com',
basePath: '/base',
},
};
}));
import { assetPath, appPath } from './path'; // transpiled path.js
var _interopRequireDefault = require('@babel/runtime-corejs2/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
value: true,
});
exports.appPath = exports.assetPath = void 0;
// ----------------------- HERE IT IS!! ----------------------------
var _config = _interopRequireDefault(require('next-server/config'));
var _getConfig = (0, _config.default)(),
publicRuntimeConfig = _getConfig.publicRuntimeConfig;
var _publicRuntimeConfig$ = publicRuntimeConfig.assetPrefix,
assetPrefix = _publicRuntimeConfig$ === void 0 ? '' : _publicRuntimeConfig$,
_publicRuntimeConfig$2 = publicRuntimeConfig.basePath,
basePath = _publicRuntimeConfig$2 === void 0 ? '' : _publicRuntimeConfig$2; // transpiled path.test.js
jest.mock('next/config', function() {
console.log('this is never run!');
return function() {
return {
publicRuntimeConfig: {
assetPrefix: 'https://example.com',
basePath: '/base',
},
};
};
});
var _path = require('./path'); Changing the mock to |
@joaovieira this is solved on next@canary |
Thanks @timneutkens! Always one step ahead. Would it be too hard to find the commit/PR that fixed it? Interested to know how it was solved. |
@joaovieira #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 👍 |
I know this closed but to the original issue of having
|
@codinronan , for my case, this was the ultimate solution for this issue. Before I've tried to add only a default empty object |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Expected Behavior
Steps to Reproduce (for bugs)
next.config.js
page
Context
publicRuntimeConfig
is undefinedThis 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.
The text was updated successfully, but these errors were encountered: