In NextJs apps that is bootstrapped via NX NextJs schematics based on instructions specified here, the
_publicRuntimeConfig_ variables specified in _next.config.js_ should be available in _getInitialProps_ during runtime both in production and dev mode as specified in orignal nextjs documentation here.
Similar project bootstrapped via create-next-app works perfectly where _publicRuntimeConfig_ variables specified in _next.config.js_ are. available in _getInitialProps_ during runtime both in production and dev mode.
Nextjs app that is created via Nx Nextjs schematics gives _undefined_ for _publicRuntimeConfig_ accessed in _getInitialProps_ during runtime of production build but in development mode it works perfectly
next.config.js
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static'
}
};
getInitialProps
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
...
Index.getInitialProps = async ctx => {
console.log('I am here');
console.log(publicRuntimeConfig.staticFolder); // Undefined when running prod build
return {};
};
Please provide detailed steps for reproducing the issue.
npm installnpm start tuskdesk
npm run build tuskdesk to create a production buildnpm run start-prod:nextjs In short it copies dist file to root and run next app (There could be a better way, I am not sure)
NX Report
@nrwl/angular : Not Found
@nrwl/cli : 9.0.4
@nrwl/cypress : 9.0.4
@nrwl/eslint-plugin-nx : 9.0.4
@nrwl/express : Not Found
@nrwl/jest : 9.0.4
@nrwl/linter : 9.0.4
@nrwl/nest : Not Found
@nrwl/next : 9.0.4
@nrwl/node : Not Found
@nrwl/react : 9.0.4
@nrwl/schematics : Not Found
@nrwl/tao : 9.0.4
@nrwl/web : 9.0.4
@nrwl/workspace : 9.0.4
typescript : 3.7.5
As I tried to reproduce the issue in bare bone NextJs setup but it works perfectly there. To reproduce follow below steps
npm installnpm run dev 
npm run build and npm run start
The issue is resolved. I was missing coping of next.config.js also while moving dist output to root.
_Instead of this_
"start-prod:nextjs": "rm -rf .next && mv dist/apps/tuskdesk .next && node ./node_modules/.bin/next start"
_Do this_
"start-prod:nextjs": "rm -rf .next && mv dist/apps/tuskdesk .next && cp apps/tuskdesk/next.config.js next.config.js && node ./node_modules/.bin/next start"
_Suggestion_
When next.config.js was missing while running the next start command, a descriptive error message could be helpful. Instead, it was using the default next.config with empty fields
Ok, this was a total pain due to it kinda just failing silently. Took me a while to realise it was NX and not something wrong with the underlying next/config and build cycle. Thank you @suwigyarathore .
Just ran into this as well and spent a few hours trying to find the issue. Thanks @suwigyarathore
Most helpful comment
The issue is resolved. I was missing coping of next.config.js also while moving dist output to root.
_Instead of this_
"start-prod:nextjs": "rm -rf .next && mv dist/apps/tuskdesk .next && node ./node_modules/.bin/next start"_Do this_
"start-prod:nextjs": "rm -rf .next && mv dist/apps/tuskdesk .next && cp apps/tuskdesk/next.config.js next.config.js && node ./node_modules/.bin/next start"_Suggestion_
When next.config.js was missing while running the next start command, a descriptive error message could be helpful. Instead, it was using the default next.config with empty fields