serverless-components / website

:zap: Instantly deploy static website on serverless infrastructure with zero configuration using Serverless Components.
https://serverless.com
Apache License 2.0
157 stars 49 forks source link

Passing variables to static files in different stages? #56

Open chusc123 opened 4 years ago

chusc123 commented 4 years ago

What's the best way to change api key or api domain uri based on a different stage in a single page application?

If this were a lambda, I would use environment variables in the serverless yml file, but since these are static files I'm not sure the best way to do this.

eahefnawy commented 4 years ago

Not sure of your framework, but you could still use environment variables with React for example: https://serverless-stack.com/chapters/environments-in-create-react-app.html

Unless I misunderstood your issue? 🤔

darbio commented 3 years ago

I think the question is:

When I have multiple stages (dev, prod) how do I define different values to my environment variables (e.g. REACT_APP_API_URL) for each stage, using the same serverless.yml.

The current answer is to use .env files, as per #44 .

dhesson commented 3 years ago

@eahefnawy Is there no way to set env vars in the serverless.yml and point them to a param from the cloud app settings? I've done this in other apps and it would be disappointing if this isn't possible:

inputs:
  env:
    AUTH_CLIENT_ID: ${param:AUTH_CLIENT_ID}
    AUTH_DOMAIN: ${param:AUTH_DOMAIN}
    AUTH_REDIRECT_URL: ${param:AUTH_REDIRECT_URL}
  src:
    src: ./src
    hook: npm run build
    dist: ./dist

My use case is the redirect URIs are different from prod and dev and this needs injected into the code at build time depending on the stage. None of these are sensitive values so it's fine that they are on the client.

If this is possible, maybe I'm trying to reference them incorrectly. Right now, I just get empty strings or undefined in the React render template.