next.js: publicRuntimeConfig shouldn’t be undefined on build

Bug report

Describe the bug

v8.1.1-canary.62 (or maybe 3aed76fad857b518a31675987e8c79f6f1afccde) (re-)introduces an issue with how I use (and recommend how to use) https://github.com/tusbar/next-runtime-dotenv.

To Reproduce

This breaks on build as publicRuntimeConfig is undefined at build time:

const {
  publicRuntimeConfig: {MY_API_URL}
} = getConfig()

Expected behavior

publicRuntimeConfig used to be {} at build time.

System information

  • OS: macOS
  • Version of Next.js: 8.1.1-canary.62

Additional context

This is not the first time this breaks, maybe it would be nice to add a test. It used to work fine with 8.0.4, it’s broken in 8.1.0 and used to work fine in canary before 8.1.1-canary.62. Broken in 9.0 as well.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 35 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I reproduced this bug on 9.4.2

I’ve tried v9.5.2 which should include #15777 and we’re still getting undefined back from any getConfig call it seems.

I also ran into this issue and in my case it was introduced by this change in the 9.4.3 release.

Since this change all undefined properties of publicRuntimeConfig are removed at build time, and if that happens for all the properties then publicRuntimeConfig is seen as {} which is in turn filtered out.

A workaround can be to add at least one property to publicRuntimeConfig that is defined at build time, could be foo:‘bar’.

In my case though the real problem was that I was not using getInitialProps as documented here.

I’m using the built in method of using publicRuntimeConfig and not the above plugin and also receive the same error message on build

Hey! I’ll release a canary now so you can use this fix.

The sentry example also experiences this error, due to the setup in the _app.tsx file.

import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
import getConfig from 'next/config'

if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
  const config = getConfig()
  const distDir = `${config.serverRuntimeConfig.rootDir}/.next`
  Sentry.init({
    enabled: process.env.NODE_ENV === 'production',
    integrations: [
      new RewriteFrames({
        iteratee: (frame: any) => {
          frame.filename = frame.filename.replace(distDir, 'app:///_next')
          return frame
        },
      }),
    ],
    dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  })
}

@egdelwonk thanks for the additional details, this makes more sense. I have submitted a PR addressing the problem here

I am having the same issue on nextjs 9.5.3 publicRuntimeConfig is not returning custom variables, but only NODE_ENV 😦

@madeinspace As jbcochery said, the real problem is allowing ASO to run on the pages where you get that error. Adding export const getServerSideProps: GetServerSideProps = async () => ({ props: {} }); to every page where that error occurred worked for me. I think it’d be great if we got a warning or an error if client side config was used on an ASO page, but it’s probably something that’s difficult to implement.

I ran into the same issue in v9.5.3 and ended up downgrading to v9.0.2 which for now suits my needs.

Edit: v9.4.0 (and v9.1.0 and v9.2.0 and v9.3.0) also works fine for me. v9.5.0 doesn’t and as mentioned above neither does v9.5.3.

Race condition seems to make sense here, if I follow the value of publicRuntimeConfig while building it changes from an empty object to undefined at some point and breaks the build.

I also received the same error message on build and think I figured it out why. It has to do with Automatic static optimization introduced in Next 9, or 8.1.1-canary.62.

Following the documentation that states “This determination is made by whether or not the page has blocking data requirements through using getInitialProps.” I ended up adding getInitialProps to every page in my application and the error went away. Since there was none getInitialProps statement in some pages, during build it would consider this pages as static and does not expose runtime env configuration.

For anyone else running into this, I ended up building the Next.js app when running my docker container (instead of when building the image) in order to avoid having to use the publicRuntimeConfig. I now just use standard build time env vars. It adds startup time, but solves this issue

I’m running into this problem as well. My project is large so a minimal repro is going to be tricky, but I suspect that this is a race condition so it may not even be possible. Running the same command (NODE_ENV=production next build) in two environments gets the same error on different pages in the app.

Early 2015 Macbook Pro (3.1 GHz Intel Core i7) I get the following:

{ TypeError: Cannot destructure property `publicRuntimeConfig` of 'undefined' or 'null'.
    at Object.tpE7 (/Users/ericbaer/dev/work/lnc/constellation/web/.next/server/static/hAFgtwbpTC55qnszuIR1i/pages/inventory.js:4011:55)

Netlify CI Environment (node10 Docker image)

TypeError: Cannot destructure property `publicRuntimeConfig` of 'undefined' or 'null'.
5:12:27 PM:     at Object.tpE7 (/opt/build/repo/web/.next/server/static/RVuMYAXq8hm_vjh7oqK4-/pages/order-acknowledgment.js:3740:55)

A few notable things about my project:

  • My project is large. There are ~90 components exported in the build.manifest, static 87Mb output dir.
  • My config is unique since I do static export with dynamic routes deployed to a CDN. You can see a description and minimal repo explaining the technique in https://github.com/zeit/next.js/pull/6963
const { set, uniq } = require(`lodash/fp`);
const { BundleAnalyzerPlugin } = require(`webpack-bundle-analyzer`);
const redirects = require(`./redirects`);

const { ANALYZE, API_ROOT } = process.env;

module.exports = {
  publicRuntimeConfig: {
    API_ROOT: API_ROOT || `http://localhost:8000/api/`,
  },
  exportPathMap: function() {
    const staticPages = uniq(redirects.map(redirect => redirect.staticPage));
    const staticExports = staticPages.reduce(
      (memo, page) => set(page, { page }, memo),
      {}
    );

    return {
      "/": { page: `/` },
      "/auth/signed-in": { page: `/auth/signed-in` },
      ...staticExports
    };
  },
  webpack: function(config, options) {
    if (!options.dev) {
      config.devtool = `nosources-source-map`;

      // Enable sourceMaps in UglifyJs
      for (const plugin of config.plugins) {
        if (plugin[`constructor`][`name`] === `UglifyJsPlugin`) {
          plugin.options.sourceMap = true;
          break;
        }
      }
    }

    return config;
  }
};