-
Notifications
You must be signed in to change notification settings - Fork 16
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
Doesn't work when a page.tsx throws an unhandled exception. #106
Comments
I think I have similar issue. When navigating to non-existing page. NextJS throw error:
Because of # .env
NEXT_PUBLIC_APP_URL=http://localhost:3000 // config.ts
import { env } from "next-runtime-env"
export const appUrl = env("NEXT_PUBLIC_APP_URL") // not-found.tsx
'use client'
import Link from "next/link"
import { appUrl } from "./config.ts"
export default function NotFound() {
console.log(window['__ENV']) // undefined
console.log(appUrl) // undefined
return <Link href={appUrl}>Back home</Link>
} |
@nekotoriy I found a workaround, and it is using the provider aproach: https://github.com/expatfile/next-runtime-env/tree/main/examples/with-app-router-context This works for my use case. NOTE: This is NOT a solution, so please don't close the issue!! |
Thanks for reporting this issue. @carlos-dubon the @nekotoriy - I don't think this issue is related, as you are using the |
I'm getting an error.
installation. |
@emreakdas - Can you please provide some additional context? |
@HofmannZ NextJS 14 I use it normally with this usage, there is nothing extra I do, but it gives the above error in the console
|
The env vars stop working once it hits a 404 page, a |
unfortunately I have given up using this package, but what is the solution? isn't this a package problem? |
@gregorybolkenstijn I will add an example to try and replicate. |
I was able to replicate the initial issue reported by @carlos-dubon. The problem appears to be that Next.js does not include the scripts from the top-level layout when a server error occurs. I attempted to address this by adding an This seems to be a problem with Next.js itself, rather than with our package, and I've opened an issue on their GitHub repository. If you'd like to keep track of its progress, please give it an upvote: vercel/next.js#63980 However, I did confirm that everything works fine on the |
This comment in the. related Next.js issue gave us an idea of how to tackle this issue: vercel/next.js#63980 (comment) The solution of adding a
So we wrapped the // app/layout.tsx
import { PublicEnvScript } from 'next-runtime-env';
export default function RootLayout({ children }) {
return (
<html lang="en">
<head>
<PublicEnvScript />
</head>
<body>
<Suspense>{children}</Suspense>
</body>
</html>
);
} It has worked well for us, we have not experienced any issues. |
that's bad for UX though, user just sees a blank page until everything is loaded. A better solution is to set up an /error/page.tsx and /not-found/page.tsx and redirect to those pages on 500 or 404, that way the scripts will load. |
The library stops working when a page.tsx throws an unhandled error. This results in a cascade of issues in my app.
Expected behaviour: read env vars even tho there is an unhandled exception in a page/layout..
The text was updated successfully, but these errors were encountered: