-
What would be the recommended way to load environment variables for both the server and client? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Do you mean at compile time or at runtime? At compile time, you can use the macros provided by the Rust standard library to embed them into the binary. At runtime, of course, you can't access env variables from inside the browser. You could read an env variable from the server in a server fn. |
Beta Was this translation helpful? Give feedback.
-
Yeah, i was wondering if perhaps there were some idiomatic way to load env variables on the server during ssr and then somehow pass them to the client (lets say an api endpoint variable, so it can easily be switched out during dev and prod stages). |
Beta Was this translation helpful? Give feedback.
Yeah so
create_resource(cx, || (), |_| async { std::env /* etc */ })
would work fine, because this will only run once, on the server, so won't panic in the browser. Remember though that this will literally send the value of the env variable as JSON to the user's browser. Don't put any secret keys in there.Otherwise, use a server function that reads the env var and uses it, sending the result and not the env var back to the client. This keeps the env var itself secret.