0
0
Astroframework~5 mins

Handling environment variables in Astro - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of environment variables in Astro projects?
Environment variables store configuration values outside the code. They help keep secrets safe and allow different settings for development and production.
Click to reveal answer
beginner
How do you access environment variables in Astro components?
Use the special import from 'astro:env' like <code>import { PUBLIC_API_URL } from 'astro:env';</code> to safely access variables starting with <code>PUBLIC_</code>.
Click to reveal answer
beginner
Why should secret environment variables NOT start with PUBLIC_ in Astro?
Variables starting with PUBLIC_ are exposed to client-side code. Secrets must not be exposed to keep them safe on the server only.
Click to reveal answer
beginner
Where do you define environment variables for an Astro project?
Create a .env file in the project root. Write variables like API_KEY=your_key_here. Use .env.production for production settings.
Click to reveal answer
intermediate
How does Astro handle environment variables during build and runtime?
Astro replaces environment variables at build time. Public variables are embedded in client code, while private ones stay on the server during build.
Click to reveal answer
Which prefix must environment variables have to be accessible in Astro client-side code?
APRIVATE_
BCLIENT_
CASTRO_
DPUBLIC_
Where should you store secret keys in an Astro project?
ADirectly in client-side JavaScript
BIn variables starting with PUBLIC_
CIn .env files without PUBLIC_ prefix
DIn HTML meta tags
How do you import environment variables in Astro components?
Aimport env from 'env';
Bimport { VAR_NAME } from 'astro:env';
Cconst VAR_NAME = process.env.VAR_NAME;
Dimport { VAR_NAME } from 'process';
What file name is used for production environment variables in Astro?
A.env.production
B.env.prod
C.env.production.local
D.env.prod.local
What happens to environment variables during Astro's build process?
AThey are replaced with their values at build time
BThey remain as placeholders in the code
CThey are encrypted and sent to the client
DThey are ignored
Explain how to safely use environment variables in an Astro project for both server and client code.
Think about how to separate public and private data.
You got /4 concepts.
    Describe the role of environment variables during Astro's build process and why it matters.
    Consider what happens before the site is served.
    You got /4 concepts.