{"success":true,"name":"treat-next-public-env-vars-as-public-in-next-js","description":"Adding or reading an environment variable in a Next.js project","category":"ai-brimind","path":"ai-brimind/treat-next-public-env-vars-as-public-in-next-js/SKILL.md","file":"SKILL.md","content":"---\nname: treat-next-public-env-vars-as-public-in-next-js\ndescription: \"Adding or reading an environment variable in a Next.js project\"\n---\n\n# Treat NEXT_PUBLIC_ Env Vars as Public in Next.js\n\n## When to use\nProject is Next.js, and you are about to add a variable to .env or prefix one with NEXT_PUBLIC_ to make it 'work' in a component.\n\n## Example request\nAPI secret key not readable in a client component, so renaming it to NEXT_PUBLIC_ → that ships the secret to every visitor's browser. — NEXT_PUBLIC means baked into the public bundle. | secret needed to call a paid API → call it from a Server Component, Server Action, or route handler where plain process.env stays on the server. — Server-side is the home of secrets. | changed an env value but the client still shows the old one → NEXT_PUBLIC_ values are inlined at build time; rebuild is required. — Build-time inlining. | Google Analytics ID or public map key in the browser → genuinely public by design, NEXT_PUBLIC_ is correct there. — Public-by-design values are the exception.\n\n## Instructions\nAct like a security guard who checks the badge of every environment variable before letting it into a Next.js project. Each time you add or read one: 1) Classify it first: is this value SECRET (API keys with billing, database URLs, tokens, passwords) or PUBLIC-BY-DESIGN (analytics IDs, public map keys, the site URL)? Say the classification explicitly. 2) Apply the rule: secrets NEVER get the NEXT_PUBLIC_ prefix — anything with that prefix is baked into the JavaScript sent to every visitor, and no trick hides it afterward. If a client component 'needs' a secret, that is a design smell: the real fix is moving the call to where Next.js already keeps things private — a Server Component, a Server Action, or a route handler that acts as a thin proxy. 3) Remember the mechanics: NEXT_PUBLIC_ values are inlined at BUILD time, so changing them needs a rebuild, and reading process.env dynamically in the browser will not work. 4) Never invent a custom 'pass the key through props or an endpoint' workaround — that is just leaking with extra steps. 5) If you are not sure whether a third-party key is safe to expose, check that provider's docs (they usually say 'publishable' vs 'secret') instead of assuming. Only add the variable once its classification is confirmed, and state in one line why it lives where it lives — server-only or public bundle — not just that the code compiles.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #486\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}