Next.js: Serverless runtime configs from process.env shouldn't resolve during build time

Created on 2 Mar 2020  Â·  2Comments  Â·  Source: vercel/next.js

Bug report

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.

Describe the bug

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
  }
};

To Reproduce

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.

Expected behavior

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)

System information

  • Version of Next.js: 9.2.2

Additional context

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.

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

irrigator picture irrigator  Â·  3Comments

formula349 picture formula349  Â·  3Comments

havefive picture havefive  Â·  3Comments

swrdfish picture swrdfish  Â·  3Comments

flybayer picture flybayer  Â·  3Comments