下一个JS publicRuntimeConfig不在运行时设置

时间:2020-09-16 15:19:49

标签: next.js

我有一个next.config.js文件,其中包含以下内容:

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的值始终为“ build”。我根本无法在运行时设置此var。如果我从构建脚本中删除CMS_BASE = build,则该值从next.config.js文件中以“ defaultValue”的形式出现。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

因此,如果其他人遇到此问题,似乎在publicRuntimeConfig内部传递默认值会覆盖您在运行时设置的值,就像在构建时设置的一样。

因此,如果您希望运行时变量起作用,请不要在定义中创建默认值,请稍后在应用程序中执行。并且不要使用同名的构建时变量。