1- // Import the functions you need from the SDKs you need
2- import { initializeApp } from "firebase/app" ;
1+ import { getApps , initializeApp , type FirebaseApp } from "firebase/app" ;
32
4- // TODO: Add SDKs for Firebase products that you want to use
5- // https://firebase.google.com/docs/web/setup#available-libraries
6-
7- // Your web app's Firebase configuration
8- const firebaseConfig = {
3+ const envConfig = {
94 apiKey : process . env . NEXT_PUBLIC_FIREBASE_API_KEY ,
105 authDomain : process . env . NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN ,
116 projectId : process . env . NEXT_PUBLIC_FIREBASE_PROJECT_ID ,
@@ -14,5 +9,36 @@ const firebaseConfig = {
149 appId : process . env . NEXT_PUBLIC_FIREBASE_APP_ID ,
1510} ;
1611
17- // Initialize Firebase
18- export const firebase = initializeApp ( firebaseConfig ) ;
12+ /** next build 등 환경 변수가 없을 때 initializeApp만 통과시키기 위한 자리 표시자(실제 Auth/네트워크 호출은 하지 않음). */
13+ const BUILD_PLACEHOLDER_CONFIG = {
14+ apiKey : "AIzaSy0000000000000000000000000000000" ,
15+ authDomain : "build-placeholder.firebaseapp.com" ,
16+ projectId : "build-placeholder" ,
17+ storageBucket : "build-placeholder.appspot.com" ,
18+ messagingSenderId : "000000000000" ,
19+ appId : "1:000000000000:web:0000000000000000000000" ,
20+ } as const ;
21+
22+ function resolveConfig ( ) {
23+ if ( envConfig . apiKey ) {
24+ return {
25+ apiKey : envConfig . apiKey ,
26+ authDomain : envConfig . authDomain ?? "" ,
27+ projectId : envConfig . projectId ?? "" ,
28+ storageBucket : envConfig . storageBucket ,
29+ messagingSenderId : envConfig . messagingSenderId ,
30+ appId : envConfig . appId ,
31+ } ;
32+ }
33+ return { ...BUILD_PLACEHOLDER_CONFIG } ;
34+ }
35+
36+ export function getFirebaseApp ( ) : FirebaseApp {
37+ const existing = getApps ( ) [ 0 ] ;
38+ if ( existing ) {
39+ return existing ;
40+ }
41+ return initializeApp ( resolveConfig ( ) ) ;
42+ }
43+
44+ export const firebase = getFirebaseApp ( ) ;
0 commit comments