Skip to content

Instantly share code, notes, and snippets.

@markomitranic
Last active February 17, 2025 06:00
Show Gist options
  • Save markomitranic/6441f312eb7d1ecce03ed8d9cd1ee53d to your computer and use it in GitHub Desktop.
Save markomitranic/6441f312eb7d1ecce03ed8d9cd1ee53d to your computer and use it in GitHub Desktop.
Unified Astro facade for accessing environment variables.
import * as client from "astro:env/client";
import type ServerEnv from "astro:env/server";
/**
* If we're in SSR, use the real server env.
* Otherwise, a proxy that throws if you try to access it.
*/
const server = import.meta.env.SSR
? await import("astro:env/server")
: (new Proxy(
{},
{
get(_target, prop) {
throw new Error(
`🚨🚨🚨 Accessing SERVER variables in client components is not permitted! Attempted to access: '${String(prop)}'.`,
);
},
},
) as typeof ServerEnv);
/**
* Access validated environment variables.
*
* Contains both client and server variables, but will not load the server
* values when rendering on the client.
*
* If server variables are accidentally accessed on the client, it will throw.
*
* @example
* env.server.API_SECRET_KEY
* env.client.API_URL
*/
export const env = {
/** Client-only environment variables. */
client,
/** Server-only env variables. Will throw if accessed on the client. */
server,
/**
* Vite/Astro's `import.meta.env`. (subset when running on the client)
* @see https://docs.astro.build/en/guides/environment-variables/#default-environment-variables
*/
meta: import.meta.env,
} as const;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment