Next.js 运行时未设置下一个JS publicRuntimeConfig
我有一个包含以下内容的next.config.js文件:Next.js 运行时未设置下一个JS publicRuntimeConfig,next.js,Next.js,我有一个包含以下内容的next.config.js文件: module.exports = { serverRuntimeConfig: {}, publicRuntimeConfig: { cmsBase: process.env.CMS_BASE || 'defaultValue', }, }; 我有两个npm脚本,用于构建和运行,如下所示: "build": "cross-env CMS_BASE=build next build", &q
module.exports = {
serverRuntimeConfig: {},
publicRuntimeConfig: {
cmsBase: process.env.CMS_BASE || 'defaultValue',
},
};
我有两个npm脚本,用于构建和运行,如下所示:
"build": "cross-env CMS_BASE=build next build",
"start": "cross-env-shell CMS_BASE=start next start"
我在我的应用程序中读取如下值:
const {publicRuntimeConfig} = getConfig();
const cmsBase = publicRuntimeConfig.cmsBase;
但cmsBase的价值始终是“构建”。我根本无法在运行时设置此变量。如果我从构建脚本中删除CMS_BASE=build,那么该值将在next.config.js文件中显示为“defaultValue”。我在这里遗漏了什么?因此,如果其他人有这个问题,似乎在publicRuntimeConfig中传递默认值会覆盖您在运行时设置的内容,就像在构建时设置的一样 因此,如果您希望运行时变量工作,请不要在定义中创建默认值,稍后在应用程序中创建。并且不要使用具有相同名称的构建时变量