Clayton
09/10/2021, 2:47 PMStaticSite
construct and curious how to connect env variables –
• Do you still need to leverage the `@serverless-stack/static-site-env`` package?
• How are you calling env vars within Svelte files - do you need to connect them via an exported props first?
Thanksthdxr
09/10/2021, 2:48 PMenvironment
prop on StaticSitethdxr
09/10/2021, 2:48 PMthdxr
09/10/2021, 2:49 PMimport.meta.env.VITE_MY_VARIABLE
Clayton
09/10/2021, 3:05 PMClayton
09/10/2021, 3:07 PMClayton
09/10/2021, 3:12 PMstatic-site-env
and prefacing the svelte-kit npm scripts with sst-env --
• exposing the vars in the StaticSite environment
prop
• capturing these in a JS module and importing them into the Svelte frontend (via approach shown in link above)
• have tried also prefacing var names with VITE_
and the vars are still showing up as undefined 🤔
Not sure yet if the vars not getting exposed is a Svelte/Vite issue or if I’m not getting them out of StaticSite/SST correctly?Clayton
09/10/2021, 3:14 PMstatic-site-env
plays in this? Is it only used to connect vars during dev or should it also be wired up to the build scripts too?Frank
environment
is configured in StaticSite ie.
environment: {
API_URL: api.url,
}
sst start
will auto-generate a mapping file inside .build
folder with the env var name API_URL
and the deployed env var value https://…
Frank
sst-env --
, all it does is:
1. look up the mapping file;
2. set the environment variables, ie. API_URL
in this case;
3. spawns a child process to run the wrapped scriptFrank
start
scripts.Clayton
09/10/2021, 6:18 PMFrank
buildCommand
for StaticSite. It will handle the replacement for you.Clayton
09/10/2021, 7:23 PMbuildCommand
-
const site = new sst.StaticSite(this, "SvelteSite", {
path: "frontend",
buildOutput: "build",
buildCommand: "npm run build",
...
Do you mean that with this you don’t need to use static-site-env
(and preface the package.json scripts with sst-env --
) or that should still be done as well?thdxr
09/10/2021, 7:34 PMClayton
09/10/2021, 7:35 PM@serverless-stack/static-site-env
package (as a dev dependency)
• update SvelteKit’s package.json dev script to sst-env -- svelte-kit dev
• expose vars within the StaticSite contruct’s environment
property as documented in SST docs
• preface all var names with VITE_
(e.g. APP_API_URL becomes VITE_APP_API_URL) to make them available to SvelteKit
• create a new .js module file that exports variables (example)
• import module into any Svelte file that needs access to variables (see example above)
• if you update vars or var names (e.g. adding VITE_ prefix) must restart SvelteKit server in addition to rebuilding any SST infrastructurethdxr
09/10/2021, 7:35 PMthdxr
09/10/2021, 7:35 PM