Closed
Description
Describe your environment
- Operating System version: Windows 10 20H2
- Browser version: Chrome 98.0.4758.102
- Firebase SDK version: 9.6.7
- Firebase Product: analytics
Describe the problem
In a NextJS app, attempting to create an Analytics
object with getAnalytics(FirebaseApp)
throws Error: Component analytics has not been registered yet
. Strangely, the other SDKs being used (auth, functions, firestore) function as expected; only analytics throws this unregistered error.
Logging the value of firebase._container.providers.get()
for a non-affected SDK (in this case, functions) shows that the component has registered properly, but for analytics the component is null
.
Steps to reproduce:
Create a new NextJS project with reactfire and the Firebase SDKs, then attempt to create an analytics provider in npm run dev
.
The code I am attempting to execute:
import {ReactNode} from 'react';
import { useFirebaseApp, useInitFirestore, AuthProvider, FunctionsProvider, FirestoreProvider, AnalyticsProvider } from 'reactfire';
import { getFirestore, initializeFirestore, enableIndexedDbPersistence, connectFirestoreEmulator } from 'firebase/firestore';
import { getAuth, connectAuthEmulator } from 'firebase/auth';
import { getFunctions, connectFunctionsEmulator } from 'firebase/functions';
import { getAnalytics } from 'firebase/analytics';
export default function FirebaseProviders(props: {children: ReactNode}) {
const firebase = useFirebaseApp();
// Initialize SDKs
const auth = getAuth(firebase);
const functions = getFunctions(firebase);
const analytics = getAnalytics(firebase);
const firestore = getFirestore(firebase);
// Set up emulators on dev build
if (process.env.NODE_ENV !== 'production') {
connectAuthEmulator(auth, 'http://localhost:9099');
connectFunctionsEmulator(functions, 'localhost', 5001);
connectFirestoreEmulator(firestore, 'localhost', 8080);
}
return (
<AuthProvider sdk={auth}>
<FunctionsProvider sdk={functions}>
<FirestoreProvider sdk={firestore}>
<AnalyticsProvider sdk={analytics}>
{props.children}
</AnalyticsProvider>
</FirestoreProvider>
</FunctionsProvider>
</AuthProvider>
)
}
which is rendered within _app.tsx
like so:
import {AppProps} from 'next/app';
import {FirebaseAppProvider} from 'reactfire';
import FirebaseProviders from '../components/firebase/FirebaseProviders';
import {fbconfig} from '../firebase/config';
export default function App({ Component, pageProps }: AppProps) {
// ...
return (
<FirebaseAppProvider firebaseConfig={fbconfig}>
<FirebaseProviders>
{/* ... */}
</FirebaseProviders>
</FirebaseAppProvider>
);
}