Tim V
03/16/2022, 2:40 PMconst site = new StaticSite(this, "website", {
path: "stack/website",
buildOutput: "dist",
buildCommand: "yarn build",
customDomain,
environment: {
API_ENDPOINT: api.url,
},
// ...
});
From package.json:
"scripts": {
"start": "sst-env -- webpack serve --open",
"build": "webpack build"
}
Any help or insight is welcomed.Frank
REACT_APP
.Frank
API_ENDPOINT
to REACT_APP_API_ENDPOINT
both in ur construct and in ur React app, and then give it a try again?Tim V
03/16/2022, 4:26 PMTim V
03/16/2022, 4:27 PMFrank
Tim V
03/16/2022, 4:27 PMTim V
03/16/2022, 4:28 PMTim V
03/16/2022, 4:28 PMTim V
03/16/2022, 4:29 PMFrank
console.log
in ur frontend code, u can see {{ API_ENDPOINT }}
in the console tab?Tim V
03/16/2022, 4:30 PMFrank
Frank
{{ API_ENDPOINT }}
in ur terminal when u build right?Tim V
03/16/2022, 4:31 PMTim V
03/16/2022, 4:31 PMTim V
03/16/2022, 4:31 PMFrank
{{ API_ENDPOINT }}
gets replaced at deploy time. Here’s how it works https://docs.serverless-stack.com/frontend#how-passing-environment-variables-worksTim V
03/16/2022, 4:34 PMTim V
03/16/2022, 4:35 PMTim V
03/16/2022, 4:35 PMTim V
03/16/2022, 4:38 PMconst site = new StaticSite(this, "website", {
path: "stack/website",
buildOutput: "dist",
buildCommand: "FOO=BAR yarn webpack build",
customDomain,
environment: {
API_ENDPOINT: api.url,
},
I can see FOO in the process.env at build time, but not the API_ENDPOINT.Tim V
03/16/2022, 4:39 PMFrank
sst start
and sst deploy
work differently.Frank
sst deploy
the value for api.url
is not available at build time.
SST builds the app with placeholders.ie.
$ API_ENDPOINT="{{ API_ENDPOINT }}" FOO=BAR yarn webpack build
Frank
After theconstruct is deployed, SST will replace all occurrences ofApi
with the real value.{{ API_ENDPOINT }}
Frank
Frank
sst deploy
, what’s value do you see for process.env.API_ENDPOINT
?Tim V
03/16/2022, 4:44 PMFrank
Tim V
03/16/2022, 4:44 PMTim V
03/16/2022, 4:45 PMFrank
API_ENDPOINT
is used in ur frontend?Tim V
03/16/2022, 4:45 PMTim V
03/16/2022, 4:45 PM<script type="module">
window.API_ENDPOINT = "{{ API_ENDPOINT }}";
</script>
Tim V
03/16/2022, 4:45 PMTim V
03/16/2022, 4:46 PMTim V
03/16/2022, 4:46 PMimport Cookies from "./Cookies";
try {
console.log("FOOBAR", process.env.API_ENDPOINT);
} catch(e) {}
const API = {};
API.invoke = (path, body) => {
const headers = new Headers();
const AccessToken = Cookies.get("AccessToken");
if (AccessToken) headers.set("Authorization", AccessToken);
return fetch(`${window.API_ENDPOINT}/${path}`, { method: "POST", headers, body: JSON.stringify(body) })
.then(response => response.json());
};
export default API;
Tim V
03/16/2022, 4:46 PMFrank
Tim V
03/16/2022, 4:47 PMTim V
03/16/2022, 4:47 PMFrank
Tim V
03/16/2022, 5:07 PMTim V
03/16/2022, 5:10 PMFrank
Frank
Tim V
03/16/2022, 5:19 PMTim V
03/16/2022, 5:20 PMFrank
auth/index.html
or index.html
at the root?Tim V
03/16/2022, 5:21 PMFrank
Tim V
03/16/2022, 5:21 PMFrank
StaticSite
currently only replaces {{ API_ENDPOINT }}
in root index.html
.Tim V
03/16/2022, 5:22 PMFrank
Frank
Tim V
03/16/2022, 5:22 PMTim V
03/16/2022, 5:23 PMFrank
Tim V
03/16/2022, 5:24 PMFrank
<script type="module">
window.API_ENDPOINT = "{{ API_ENDPOINT }}";
</script>
Frank
window.API_ENDPOINT
should give u the deployed URLTim V
03/16/2022, 5:25 PMTim V
03/16/2022, 5:25 PMTim V
03/16/2022, 5:25 PMTim V
03/16/2022, 5:26 PMTim V
03/16/2022, 5:26 PMFrank
process.env.API_ENDPOINT
..Frank
Frank
process.env.API_ENDPOINT
is not being resolved by the buildFrank
$ API_ENDPOINT="{{ API_ENDPOINT }}" FOO=BAR yarn webpack build
Does process.env.API_ENDPOINT
not get resolved to {{ API_ENDPOINT }}
?Tim V
03/16/2022, 5:33 PMFrank
$ API_ENDPOINT="{{ API_ENDPOINT }}" yarn webpack build
Frank
dist
folder, do u see process.env.API_ENDPOINT
or do u see "{{ API_ENDPOINT }}"
?Tim V
03/16/2022, 5:36 PMtry {
console.log("FOOBAR", process.env.API_ENDPOINT);
} catch(e) {}
Tim V
03/16/2022, 5:37 PM$ API_ENDPOINT="{{ API_ENDPOINT }}" yarn webpack build
?Tim V
03/16/2022, 5:37 PMFrank
Frank
Tim V
03/16/2022, 5:38 PMTim V
03/16/2022, 5:38 PMTim V
03/16/2022, 5:38 PMTim V
03/16/2022, 5:38 PMTim V
03/16/2022, 5:39 PMFrank
$ API_ENDPOINT="{{ API_ENDPOINT }}" yarn webpack build
and then look at the output JS file in dist
, do you see
try {
console.log("FOOBAR", process.env.API_ENDPOINT);
} catch(e) {}
or
try {
console.log("FOOBAR", "{{ API_ENDPOINT }}");
} catch(e) {}
Tim V
03/16/2022, 5:54 PMTim V
03/16/2022, 5:55 PMTim V
03/16/2022, 5:55 PMFrank
$ API_ENDPOINT="{{ API_ENDPOINT }}" yarn webpack build
produces
try {
console.log("FOOBAR", "{{ API_ENDPOINT }}");
} catch(e) {}
right?Tim V
03/16/2022, 5:59 PMFrank
dist
folder and see if there is any occurrence of process.env.API_ENDPOINT
not being replaced with "{{ API_ENDPOINT }}"
?Tim V
03/16/2022, 6:00 PMTim V
03/16/2022, 6:00 PMTim V
03/16/2022, 6:00 PMTim V
03/16/2022, 6:01 PMFrank
process.env.API_ENDPOINT
in there. Do you know why that’s happening?Tim V
03/16/2022, 6:01 PMTim V
03/16/2022, 6:03 PMTim V
03/16/2022, 6:03 PMTim V
03/16/2022, 6:03 PMconst templateParameters = {};
templateParameters.API_ENDPOINT = process.env.API_ENDPOINT;
Tim V
03/16/2022, 6:03 PMTim V
03/16/2022, 6:04 PMFrank
{{ API_ENDPOINT }}
in the JS file, that should get replaced with the deployed url.Tim V
03/16/2022, 6:04 PMFrank
Tim V
03/16/2022, 6:05 PMFrank
{{ API_ENDPOINT }}
gets replaced in index.html
and in ALL js files.Tim V
03/16/2022, 6:06 PMTim V
03/16/2022, 6:08 PMconsole.log("{{ API_ENDPOINT }}");
Tim V
03/16/2022, 6:08 PMFrank
Tim V
03/16/2022, 6:09 PMTim V
03/16/2022, 6:25 PMFrank
Auth
page, instead of setting module.API_ENDPOINT
in the html file. Can you try using process.env.API_ENDPOINT
directly in JS?Frank
Tim V
03/16/2022, 6:46 PMTim V
03/16/2022, 6:48 PMTim V
03/16/2022, 6:48 PMFrank
0.70.0-next.5
Can you give it a try and let me know if it works for you?Tim V
03/16/2022, 7:06 PMTim V
03/16/2022, 7:09 PMFrank
Frank
Tim V
03/16/2022, 7:13 PMTim V
03/16/2022, 7:14 PMFrank
Tim V
03/16/2022, 7:36 PMFrank
Tim V
03/16/2022, 7:40 PMTim V
03/16/2022, 7:40 PMFrank
Frank
Tim V
03/18/2022, 2:14 PMTim V
03/22/2022, 7:23 PMTim V
03/22/2022, 8:28 PMFrank
0.69.3
Tim V
03/25/2022, 2:19 PM