Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless runtime configs from process.env shouldn't resolve during build time #10791

Closed
davidknezic opened this issue Mar 2, 2020 · 3 comments · Fixed by #10792
Closed
Milestone

Comments

@davidknezic
Copy link

Bug report

I was happy to learn that next@9.2.2 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

500

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.

@ijjk ijjk added please add a complete reproduction The issue lacks information for further investigation and removed please add a complete reproduction The issue lacks information for further investigation labels Mar 2, 2020
@ijjk
Copy link
Member

ijjk commented Mar 2, 2020

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!

@Timer Timer added this to the 9.3.0 milestone Mar 2, 2020
@Timer
Copy link
Member

Timer commented Mar 2, 2020

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.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants