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

[Proposal] Support changing the environment variables at runtime without necessarily rebuilding the app #823

Open
hannojg opened this issue Dec 6, 2024 · 1 comment

Comments

@hannojg
Copy link

hannojg commented Dec 6, 2024

What?

Enable runtime updates of environment variables in React Native applications. When .env files are modified while the app is running, the changes will be reflected in JavaScript immediately without requiring a rebuild. Note that native-side environment variables will still require a rebuild to update, as they are compile-time values.

Motivation

Building React Native apps can be time-consuming, especially for larger projects. Many applications only use environment variables in JavaScript code, making runtime updates valuable for development workflows. Currently, even simple environment variable changes require a full rebuild.

Technical Considerations

  1. I am not sure if the current behavior (requiring rebuilds) is intentional, or merely due to technical reasons? Any relevant previous discussions or issues would be helpful context (i haven't found anything on a quick search)!
  2. There's a potential for confusion if JavaScript and native environment variables become out of sync. However, developers using native-side environment variables typically understand that changes would require a rebuild.

Proposed Implementation

Create a Metro plugin that:

  • Watches .env files for changes
  • Serves dynamic environment values to JavaScript
  • The library code internally would first check if the import from JS is available, but otherwise fallback to the native values. Something like importFromJs ?? envVarsFromNativeSide
  • In release we would always use the envs from the native side

[Note: i haven't looked very closely at the code of this lib yet, let me know if there are reasons why this solution wouldn't work.]

Making this a Metro plugin would:

  • Keep the feature opt-in
  • Preserve existing behavior for users who don't configure the plugin, which would avoid breaking changes when someone updates to the latest version

I'm willing to implement this functionality as I have a direct use case for it. I just wanted to discuss this with the maintainers first, so let me know what you think please ✌️

@hannojg
Copy link
Author

hannojg commented Dec 6, 2024

Just stumbled on:

hm, so maybe we would leave it to the users if they want to implement such a behaviour themself they could fallback to use this library

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

No branches or pull requests

1 participant