I was happy to learn that [email protected] introduced runtime config support for serverless targets:
Add support for runtimeConfigs in serverless mode: #10365
Make sure runtime config is set before any imports for serverless: #10386
Make sure runtime config works in dev mode for serverless targ…: #10402
However, after giving it a try I believe that there are some inconsistencies compared to the handling in server targets.
When I specify my runtime configs in next.config.js, the process.env variables seem to be resolved during build time instead run time. Switching the target back to server makes the env vars resolve during runtime again.
module.exports = {
target: 'serverless',
serverRuntimeConfig: {
secret: process.env.MY_SECRET
},
publicRuntimeConfig: {
uri: process.env.MY_URI
}
};
git clone https://github.com/davidknezic/now-nextjs-serverless-runtime-config-issue
yarn install
yarn run next build
MY_URI=uri MY_SECRET=secret yarn run next start
The index page will return Hello undefined undefined.
I'd expect the page to return Hello secret uri when rendered on server, and Hello undefined uri when rendered on the client. (due to lack of server runtime config on client)
Additionally, there is another issue when deploying that project to Zeit Now. While now dev seems ok, the productive deployment fails with
Duration: 37.84ms
Memory Used: 80 MB
ID: q8s27-1583164652631-baa9aa7de573
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15
2020-03-02T15:57:33.750Z 7e204fe5-a1fb-4266-89de-f0b72bdc4a05 ERROR TypeError: Cannot destructure property `serverRuntimeConfig` of 'undefined' or 'null'.
at IndexPage (/var/task/.next/serverless/pages/index.js:689:9)
at d (/var/task/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:498)
at $a (/var/task/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:39:16)
at a.b.render (/var/task/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:476)
at a.b.read (/var/task/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
at renderToString (/var/task/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:364)
at render (/var/task/node_modules/next/dist/next-server/server/render.js:81:16)
at Object.renderPage (/var/task/node_modules/next/dist/next-server/server/render.js:346:16)
at Function.getInitialProps (/var/task/.next/serverless/pages/index.js:861:19)
at Object.loadGetInitialProps (/var/task/node_modules/next/dist/next-server/lib/utils.js:59:29)
The combination of target: serverless and the @now/next builder seem to return null or undefined from getConfig(). Unfortunately, I don't know how to properly test a productive Now deployment locally. I've committed the now.json to reproduce this additional issue, too.
Hi, it looks like there was a mismatch with next/config and the new experimental-serverless-trace mode which is used when deploying on Now. I submitted a PR to address this, thanks for the report!
Just to be clear, we do not recommend using serverRuntimeConfig or publicRuntimeConfig in most cases.
You should be using pure process.env and the env key instead, respectively.
This support was strictly added for backwards compatibility.
Most helpful comment
Just to be clear, we do not recommend using
serverRuntimeConfigorpublicRuntimeConfigin most cases.You should be using pure
process.envand theenvkey instead, respectively.This support was strictly added for backwards compatibility.