Skip to content

Commit 6d98ef2

Browse files
AchoArnoldCopilot
andcommitted
fix(web): await compat SDK initialization to prevent race condition
The FirebaseAuth component calls firebase.auth() synchronously on mount, but the compat SDK was being initialized in a non-blocking .then() callback. Make the plugin async so the compat app is ready before components mount. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c291ad3 commit 6d98ef2

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

web/app/plugins/firebase.client.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FirebaseApp } from "firebase/app";
33
import { getAuth, onAuthStateChanged } from "firebase/auth";
44
import { useAuthStore } from "../stores/auth";
55

6-
export default defineNuxtPlugin(() => {
6+
export default defineNuxtPlugin(async () => {
77
const config = useRuntimeConfig();
88
const publicConfig = config.public as Record<string, string>;
99

@@ -32,18 +32,15 @@ export default defineNuxtPlugin(() => {
3232

3333
// Also initialize the compat SDK for FirebaseUI
3434
if (import.meta.client) {
35-
import("firebase/compat/app").then(
36-
(firebase: {
37-
default: {
38-
apps: unknown[];
39-
initializeApp: (config: typeof firebaseConfig) => void;
40-
};
41-
}) => {
42-
if (!firebase.default.apps.length) {
43-
firebase.default.initializeApp(firebaseConfig);
44-
}
45-
},
46-
);
35+
const firebase = (await import("firebase/compat/app")) as {
36+
default: {
37+
apps: unknown[];
38+
initializeApp: (config: typeof firebaseConfig) => void;
39+
};
40+
};
41+
if (!firebase.default.apps.length) {
42+
firebase.default.initializeApp(firebaseConfig);
43+
}
4744
}
4845

4946
// Listen for auth state changes and update the auth store

0 commit comments

Comments
 (0)