Search code examples
typescriptastrojs

How to define custom config / env variables / constants in Astro Build


I'm aware of The Astro Config File and Environment Variables support in Astro.

But imagine I have a custom config option like siteTitle, siteDescription, analyticsId, enableBlackFridayDeals or employeeIdOfTheMonth which I want to configure in some common config file.

Is it possible to add custom config options to The Astro Config File and retrieve their values in the generated pages?

The Environment Variables do support this but it feels this is more for secret key/values.

How to configure these website specific configuration values/constants in Astro? And to retrieve them type safe.


Solution

  • You could use a .env file together with the dotenv module but if you feel like this is "too secret" you could just use a .ts file which exports your config options.

    src/config.ts

    export const config = {
      siteTitle: "My Site Title"
    }
    
    // or
    
    export const siteTitle = "My Site Title"