Next.js Nextjs中的publicRuntimeConfig客户端不工作
在我的Next.js Nextjs中的publicRuntimeConfig客户端不工作,next.js,Next.js,在我的next.config.js中,我有以下配置: module.exports = { publicRuntimeConfig: { // Will be available on both server and client GRAPHQL_URL: process.env.API_URL, }, } 我的脚本如下所示: "build": "next build", &quo
next.config.js中,我有以下配置:
module.exports = {
publicRuntimeConfig: {
// Will be available on both server and client
GRAPHQL_URL: process.env.API_URL,
},
}
我的脚本如下所示:
"build": "next build",
"start": "API_URL=some-url next start",
现在运行npm运行start
并在我的代码中使用{publicRuntimeConfig}=getConfig()
publicRuntimeConfig
包含GRAPHQL\u URL
服务器端,但它在客户端是一个空对象。是否使用getInitialProps
<除非使用getInitialProps
,否则code>publicRuntimeConfig
将不可用:
依赖publicRuntimeConfig的页面必须使用getInitialProps
选择退出自动静态优化。运行时配置将不起作用
可用于任何页面(或页面中的组件),而无需
getInitialProps