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
- fix https://github.com/vercel/next.js/issues/7713#issuecomment-643632270 — committed to jtaox/next.js by jtaox 6 years ago
- add test case for runtime-config in the development environment https://github.com/vercel/next.js/issues/7713#issuecomment-643632270 — committed to jtaox/next.js by jtaox 6 years ago
- fixed issue with runtime-config returning undefined when building or in a development environment (#15777) fix https://github.com/vercel/next.js/issues/7713#issuecomment-643632270 — committed to vercel/next.js by jtaox 6 years ago
I reproduced this bug on 9.4.2
I’ve tried v9.5.2 which should include #15777 and we’re still getting
undefinedback from anygetConfigcall 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
publicRuntimeConfigare removed at build time, and if that happens for all the properties thenpublicRuntimeConfigis seen as{}which is in turn filtered out.A workaround can be to add at least one property to
publicRuntimeConfigthat is defined at build time, could be foo:‘bar’.In my case though the real problem was that I was not using
getInitialPropsas 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.
@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
publicRuntimeConfigwhile building it changes from an empty object toundefinedat 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, or8.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 issueI’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:
Netlify CI Environment (node10 Docker image)
A few notable things about my project:
build.manifest, static 87Mb output dir.