Skip to content

Instantly share code, notes, and snippets.

@bramus
Created October 17, 2021 09:13
Show Gist options
  • Save bramus/093dadabcac610c58a3e133b64f97417 to your computer and use it in GitHub Desktop.
Save bramus/093dadabcac610c58a3e133b64f97417 to your computer and use it in GitHub Desktop.
Astro Env Vars
  • Install @snowpack/plugin-dotenv

    npm i --save @snowpack/plugin-dotenv
    
  • Configure Astro via astro.config.mjs to use @snowpack/plugin-dotenv

    plugins: [
      "@snowpack/plugin-dotenv",
    ],
  • Expose Env Vars via Snowpack's snowpack.config.mjs. Vars must be prefixed with SNOWPACK_PUBLIC_

    export default {
      env: {
        SNOWPACK_PUBLIC_API_BASEURL: 'https://myapi.example.org/',
      },
    };
  • Access the Env Vars on the __SNOWPACK_ENV__ object

    const res = await fetch(`${__SNOWPACK_ENV__.SNOWPACK_PUBLIC_API_BASEURL}/events`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment