Hello everyone,
I’m hitting a wall with Firebase App Hosting for my Next.js (App Router) application, and I’ve exhausted all troubleshooting steps. I’m seeing a persistent issue where process.env.NEXT_PUBLIC_ environment variables are undefined at runtime in the deployed app, causing client-side crashes, even though they are correctly configured in apphosting.yaml.
Summary of the Problem:
- My Next.js App Router app builds successfully on Firebase App Hosting.
- However, when the app loads in the browser, I get a client-side error:
"Firebase API Key is missing. Check your .env.local file and next.config.ts env property." - My server-side API routes that attempt to read these same
NEXT_PUBLIC_variables also find themundefined, leading to 500 errors. - The only way to get the app functional is by hardcoding the public Firebase client configuration directly into
src/lib/firebase.ts.
Environment:
- Next.js Version: 15.3.5 (as per build logs)
- Firebase JS SDK: (Latest stable versions used)
- Deployment Method: GitHub integration with Automatic Rollouts to
masterbranch.
Detailed Troubleshooting Steps & Evidence (What I’ve Already Tried):
apphosting.yamlConfiguration:
-
I’ve meticulously configured my
apphosting.yaml(renamed fromapphosting.staging.yamlfor clarity). -
All
NEXT_PUBLIC_variables (e.g.,NEXT_PUBLIC_FIREBASE_API_KEY,NEXT_PUBLIC_APP_URL) are defined underrunConfig.envwith **explicit `availability: [“BUILD”, “RUNTIME”]. -
Example snippet from
apphosting.yaml:`YAMLrunConfig:
…
env:
- variable: NEXT_PUBLIC_FIREBASE_API_KEY
value: “YOUR_FIREBASE_WEB_API_KEY_HERE” # Placeholder for actual key
availability: [“BUILD”, “RUNTIME”]
# … all other NEXT_PUBLIC_ variables are similarly defined
# … and other private secrets also with availability: [“RUNTIME”]`
-
next.config.ts Configuration:
I’ve added an explicit
envblock innext.config.tsto instruct Next.js to inline these variables, as commonly suggested forNEXT_PUBLIC_variables.Example snippet from next.config.ts : TypeScript// next.config.ts
const nextConfig: NextConfig = {
env: {
NEXT_PUBLIC_FIREBASE_API_KEY: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
// … all other NEXT_PUBLIC_ variables
},
// … rest of config
};` -
Code Access & Guards (
src/lib/firebase.ts):
- My Firebase initialization is client-side (
"use client"components,typeof window !== 'undefined'guards) and uses lazy, on-demand getters. - My code accesses variables directly via
process.env.NEXT_PUBLIC_VARIABLE_NAME.
- Debugging
process.env(Cloud Run Logs - CRITICAL EVIDENCE):
- I created a simple API route (
/api/firebase-client-config) that just logs and returnsprocess.env.NEXT_PUBLIC_FIREBASE_API_KEY. - Cloud Run logs for this API route consistently show
apiKey: undefined(and otherNEXT_PUBLIC_vars asundefined), even though the API route runs on the deployed server (Cloud Run instance). - This API route returns a 500 error because of these missing variables, which then causes the client-side error.
- Cloud Build Logs (Pre-Deployment - CRITICAL EVIDENCE):
- Cloud Build logs show that the
apphosting.yamlis read. - However, in the
Final app hosting schemaoutput of the “preparer” step,FIREBASE_WEBAPP_CONFIG(derived from myNEXT_PUBLIC_variables) only showsavailability: - BUILD, despite my YAML explicitly requesting["BUILD", "RUNTIME"].FIREBASE_CONFIGcorrectly shows["BUILD", "RUNTIME"].
- **Deployed Cloud Run Service YAML (Final Manifest)
- I extracted the actual Cloud Run Service YAML for my deployed service.
- In the
spec.template.spec.containers.envblock,FIREBASE_WEBAPP_CONFIGand its individualNEXT_PUBLIC_components are COMPLETELY ABSENT. OnlyFIREBASE_CONFIGis present.
The Core Problem:
It appears there is a bug in Firebase App Hosting’s current implementation for Next.js where it fails to consistently inject NEXT_PUBLIC_ (and other runConfig.env) environment variables into the process.env of the deployed Cloud Run container at runtime, despite being correctly configured in apphosting.yaml with availability: ["BUILD", "RUNTIME"].
Current Workaround:
My application is currently functional only because I have hardcoded the public Firebase client configuration directly into src/lib/firebase.ts. This is not a sustainable or secure long-term solution.
Has anyone else encountered this precise issue where NEXT_PUBLIC_ environment variables, correctly configured in apphosting.yaml with availability: ["BUILD", "RUNTIME"], are simply not present in process.env of the deployed Cloud Run container?
Any insights, workarounds, or confirmation of this bug would be immensely helpful. I’m looking for a way to get Firebase App Hosting to reliably inject these variables without hardcoding