Skip to content
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

Dynamic client side envs #79

Closed
wants to merge 3 commits into from
Closed

Conversation

JasonMHasperhoven
Copy link
Contributor

@JasonMHasperhoven JasonMHasperhoven commented Oct 1, 2024

As per @conorsch 's request, this adds support to set envs dynamically post-build.

  • Routes need to pass envs from getServerSideProps as props
  • _app will take this and provide it as a context (in v2 we could use mobx or whatever state manager we decide on)
  • Components can consume useEnvContext to access the envs

@JasonMHasperhoven JasonMHasperhoven requested review from a team and conorsch October 1, 2024 15:34
@JasonMHasperhoven
Copy link
Contributor Author

I'm still on the fence whether we should do this, @grod220 any thoughts?

@JasonMHasperhoven JasonMHasperhoven marked this pull request as ready for review October 1, 2024 15:39
const envs = whitelist.reduce(
(envs: Record<string, string>, key) => ({
...envs,
[key]: process.env[key] ?? '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why using context instead of just exporting envs as consts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to store it somewhere since we receive it from the server-side, this is a temporary solution as we're still debating the state manager.


export async function getServerSideProps() {
const envs = getClientSideEnvs();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: instead of calling this function on every page, expose selected environmental variables with NEXT_PUBLIC_ prefix. See nextjs docs about runtime envs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NEXT_PUBLIC_ envs will be compiled within the build unfortunately. So you can not provide an env var starting with NEXT_PUBLIC_ after the build is complete and let nextjs use that.

@@ -0,0 +1,13 @@
export function getClientSideEnvs() {
const whitelist: string[] = ['PENUMBRA_CHAIN_ID', 'PENUMBRA_CUILOA_URL'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: add .env.example file to env. It shouldn't have any values, just keys with comments on what they do, and where to get them from

@grod220
Copy link
Contributor

grod220 commented Oct 2, 2024

It's tough for me to review this with all the formatting changes. Could you open another PR that just does the formatting (and adds CI/CD checks for prettier)?

@JasonMHasperhoven
Copy link
Contributor Author

JasonMHasperhoven commented Oct 2, 2024

It's tough for me to review this with all the formatting changes. Could you open another PR that just does the formatting (and adds CI/CD checks for prettier)?

I didn't wanna do it, but I did it: #80

@JasonMHasperhoven
Copy link
Contributor Author

Closing this one.

@grod220
Copy link
Contributor

grod220 commented Oct 2, 2024

I didn't wanna do it, but I did it: #80

This isn't the formatting change PR though right? I think those changes are important and there should be a CI/CD test like in penumbra-zone/web.

@grod220
Copy link
Contributor

grod220 commented Oct 2, 2024

If we merged that separately, this PR would have automatically been cleared up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants