Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
Reuse firebase object instance in multiple components

I am playing around with Firebase and Svelte at the moment and wondering about the best way to set up a Firebase app object that I can use from several components. For example, I have two different components/pages (client side routing) that both need to get some data from Firestore. Currently, I am initializing the app object in both pages and they are doing their own thing after that, something like this:

// PageB.svelte
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    // ...
};

// Initialize Firebase app
const app = initializeApp(firebaseConfig);
// Initialize Firestore
const db = getFirestore(app);

// Do something with the db object..
// PageA.svelte
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    // ...
};

// Initialize Firebase app again
const app = initializeApp(firebaseConfig);
// Initialize Firestore again
const db = getFirestore(app);

// Do something with the db object..

I am initializing and creating two objects exactly the same in the components and this feels wrong and I would at least break out the config to a global config of some kind but I am not sure what to do with the Firebase objects.

Should I create a wrapper around the initialization and then import that into my pages or what would be the recommended solution? Something like this?:

// firebase.ts
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    // ...
};

export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);
// PageA.svelte
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    // ...
};

// Initialize Firebase app again
const app = initializeApp(firebaseConfig);
// Initialize Firestore again
const db = getFirestore(app);

// Do something with the db object..

Is there a problem with initializing a firebase app several times or is it a problem to share the same instance? I am leaning towards the multi use instance at the moment since that would allow me to easily attach emulators in the dev environment as well but I am not sure what is the best approach.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You like to share firebase objects. So share the instance. The best way to share a firebase instance is to use a firebase.js (like your example firebase.ts) to init the instance (app).

As soon as you import the firebase.js (FOR THE FIRST TIME) the instance (app) will be available and will be shared bacause an ES6 module will run once. Consecutive imports will share the same app instance.

firebase.js :

export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

PageA.svelte :

import { db } from "../firebase"
... // use the db

PageB.svelte :

import { db } from "../firebase"
... // use the db

You can use env variables in your firebase.js to control the usage of emulators

firebase.js vite example:

if (import.meta.env.DEV) {
  connectFirestoreEmulator(db, "localhost", 8080);
  connectStorageEmulator(storage, "localhost", 9199);
}
about 4 years ago · Santiago Trujillo Relatório

0

In most cases, you only have to initialize a single, default app. There's a chance that you might encounter a problem if it's the same instance. Mostly multiple initialization is for using another project.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda