https://serverless-stack.com/ logo
#help
Title
# help
j

Jan Plaček

03/14/2022, 9:20 PM
How do I pass env variable to static site that depends on (the same) static site ? 😆 I need to set
Copy code
API_URL: `${staticSite.url}/api`
t

thdxr

03/14/2022, 9:20 PM
That should work hypothetically
does it not?
j

Jan Plaček

03/14/2022, 9:22 PM
Well I have to provide these to
StaticSite
constructor... it's chicken-egg problem...
would either need to define
environment
and
replaceValues
as callbacks or have
staticSite.setEnvironment
and
staticSite.setReplaceValues
t

thdxr

03/14/2022, 10:28 PM
Oh I missed the part where you said "the same"
can you not precompute the url? You're relaying on the randomly generated cfn url?
j

Jan Plaček

03/15/2022, 7:48 AM
Generated for devel, for prod the domain is known. (You did not miss that part, I edited the comment to make it clearer 🙂 )
f

Frank

03/16/2022, 7:09 PM
@Jan Plaček can you give this a try?
Copy code
import { Lazy } from "aws-cdk-lib";
import * as sst from "@serverless-stack/resources";

export class MainStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string) {
    super(scope, id);

    let site;

    // Create StaticSite
    site = new sst.StaticSite(this, "Frontend", {
      path: "src/sites/website",
      environment: {
        API_URL: Lazy.stringValue({
          produce(context) {
            return site.url;
          }
        })
      },
    });
  }
}
j

Jan Plaček

03/16/2022, 7:19 PM
Ah, interesting, I definitely will, I want to solve it somehow, just not sure when as atm I am burdened by some other issues. Btw one other case I was thinking about was using staticSite.url in API gateway CORS list and api.url in StaticSite ... similar problem with cyclic depedency.
t

thdxr

03/16/2022, 7:20 PM
btw for this specific situation I usually just pull the url from
location
in js
j

Jan Plaček

03/16/2022, 7:24 PM
Unless I am missing something, that wouldn't work on devel, because the site is served from
localhost
, but API is on
actualGeneratedStaticSiteDomain/api
Atm if I am on devel (sst start), I set the API_URL to api.url. So the request doesn't go through CF (StaticSite). But it also means if there is an error or some incorrectly configured cache policy or something related to this integration, I won't be able to spot this and debug this on devel. And it was actually quite tricky to get it right.
CF strips all sorts of information from request/response and it could also be configured to modify the request (eg path) - I would have to simulate this somehow on devel or configure gateway differently etc.
f

Frank

03/16/2022, 8:21 PM
Oh r u routing the api calls through the CF distribution? Any reason not having the frontend hit the backend url directly?
j

Jan Plaček

03/16/2022, 8:44 PM
Because the api and site must be on the same domain (on production/deployment)
so I either have to route domian -> CF -> API or domain -> API -> CF (the first is the "correct" approach from what I could find)
f

Frank

03/16/2022, 9:38 PM
I see. Is
<http://domain.com|domain.com>
and
<http://api.domain.com|api.domain.com>
not an option?
ah i see it’s been asked in the other thread
4 Views