Losing environment variables after time of inactivity

84 Views Asked by At

I need some help because I'm struggling with my react native app, I develop it using expo and build it using eas.

I'm using environment variables declared on my eas.json, pointed into my app.config.ts and retrieved using expo-constants as declared into expo doc.

Everything works apparently fine, I can reach both environment variables and secrets. But don't know why, after some times with the app closed or in background, it stop working, or actually it loses both env variables and secrets (until now I did not understand how long it takes and what it depends on).

Here is my eas.json:

{
  "cli": {
    "version": ">= 2.4.0"
  },
  "build": {
    "development": {
      "developmentClient": true,
      "distribution": "internal",
      "env": {
        "API_URL": "devUrlForAPI",
        "ENV": "DEVELOPMENT"
      }
    },
    "staging": {
      "distribution": "internal",
      "android": {
        "buildType": "apk"
      },
      "env": {
        "API_URL": "stgUrlForAPI",
        "ENV": "STAGING"
      }
    },
    "production": {
      "env": {
        "API_URL": "prodUrlForAPI",
        "ENV": "PRODUCTION"
      }
    }
  },
  "submit": {
    "production": {}
  }
}

And this is my app.config.ts:

import { ConfigContext, ExpoConfig } from "@expo/config";
import "dotenv/config";

export default ({ config }: ConfigContext): ExpoConfig => ({
  ...config,
  name: "AppName",
  slug: "AppSlug",
  version: "1.0.0",
  orientation: "portrait",
  icon: "./assets/icon.png",
  splash: {
    image: "./assets/splash.png",
    resizeMode: "contain",
    backgroundColor: "#DEA148",
  },
  updates: {
    fallbackToCacheTimeout: 0,
  },
  assetBundlePatterns: ["**/*"],
  ios: {
    buildNumber: "1.0.0",
    supportsTablet: false,
    config: {
      googleMobileAdsAppId: process.env.ANDROID_ADMOB, // secrets
    },
  },
  android: {
    versionCode: 1,
    config: {
      googleMobileAdsAppId: process.env.IOS_ADMOB, // secrets
    },
    package: "com.name.appName",
    googleServicesFile: "./google-services.json",
  },
  plugins: [
    [
      "expo-notifications",
      {
        icon: "./assets/icon.png",
        color: "#FFFFFF",
      },
    ],
  ],
  notification: {
    icon: "./assets/notifyIcon.png",
  },
  extra: {
    apiUrl: process.env.API_URL,
    env: process.env.ENV,
    eas: {
      projectId: "...projectId...",
    },
    admob: {
      ios: process.env.IOS_ADMOB,
      android: process.env.ANDROID_ADMOB,
    },
  },
});

Inside the app I retrieve these env variables as shown below:

import * as Constants from "expo-constants";

const API_URL = Constants.manifest?.extra?.apiUrl;
const ADMOB_KEY = Constants.manifest?.extra?.admob.ios. // or android

In the end pointing to the manifest. Debugging this behavior, I realized (logging the manifest in the console) that at the first time the manifest looks like the app.config.ts with values for all the env variables (apiUrl and admob.ios/android). Instead when the app breaks the manifest looks like before but showing only static value (under extra I can only see the projectId) plus the paths to all the resources (like fonts and images).

Has this happen to anyone? I'm really becoming crazy on that, please help me!

Let me also know if more information could be useful. Thank you in advance!

0

There are 0 best solutions below