expatfile / next-runtime-env

Next.js Runtime Environment Configuration - Populates your environment at runtime rather than build time.
https://www.npmjs.com/package/next-runtime-env
MIT License
454 stars 18 forks source link

Nextjs 14.2.5 (and older): notfound() / not-found client components don't have access to the environment variables. #147

Open agustingabiola opened 3 months ago

agustingabiola commented 3 months ago

Issue

When a page defaults to the not-found page client components fail to load the environment variables (all NEXT_PUBLIC) are undefined while on the server they have the correct values.

This is happening in all 3.X versions I tried and with Nextjs app router v14

Unsleeping commented 2 months ago

Even if the not-found component is server-side, after the redirect to the page, the entire window.__ENV becomes undefined, and env("ENV_KEY") also becomes undefined.

fixes with changing router.push() to window.location.href without page refresh it works as expected

only happens from navigation from not-found page

Unsleeping commented 2 months ago

@agustingabiola Hi! Since my PR #150 hasn't received a response, I've published my fork on npm.

You can switch to awesome-next-runtime-env and be sure to configure the strategy prop for the assignment in your Root Layout:

<head>
   <PublicEnvScript strategy="afterInteractive" />
 </head>

For more information about the issue, check out: https://github.com/vercel/next.js/issues/69567#issuecomment-2334542530 Forked version: https://github.com/expatfile/next-runtime-env/pull/150