Replies: 1 comment 2 replies
-
If you return the env variables always from your root loader, you can use the // app/root.tsx
export async function loader() {
return json({
env: {
something: process.env.SOMETHING!,
anotherThing: process.env.ANOTHER_THING!,
}
});
}
// rest of the root code here Then you can create a hook to access import { useRouteLoaderData } from "@remix-run/react";
import type { loader } from "~/root";
export function useEnv() {
let data = useRouteLoaderData<typeof loader>("root")!;
return data.env;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've read https://remix.run/docs/en/main/guides/envvars for the recommended way to expose environment variables in the browser.
I am taking a slightly modified approach and wanted to share it here.
First, I defined
app/providers/client-environment
as:Then, the client environment provider can be utilized in the root:
Now that the root wraps the
Outlet
withinClientEnvironmentProvider
, any route and component downstream can useuseClientEnvironment
:It is easy enough to do this in individual projects, but there also might be an opportunity to streamline the
ClientEnvironmentProvider
anduseClientEnvironment
so that they are exported from one ofremix
modules if you find my approach valuable.Beta Was this translation helpful? Give feedback.
All reactions