Created
January 19, 2024 04:12
-
-
Save ChrisChiasson/53d09a494ae0143b5ea4c71649321fb1 to your computer and use it in GitHub Desktop.
Cleaned up Firebase V8 Lazy Loading Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// firebaseApp replaces import firebase from "firebase/app" (the _only_ named import) | |
// firebaseAppPromise is the promise that gets awaited immediately to prevent multi-call re-init | |
let firebaseApp, firebaseAppPromise; | |
export async function getFirebaseApp() { | |
if (!firebaseApp) { | |
if (!firebaseAppPromise) firebaseAppPromise = import(/*webpackPreload: true*/ "firebase/app"); | |
firebaseApp = await firebaseAppPromise; | |
} | |
return firebaseApp; | |
} | |
const config = { | |
//... | |
} | |
let app, appPromise; | |
export async function getApp() { | |
if (!app) { | |
if (!appPromise) appPromise = getFirebaseApp().then(firebase => firebase.initializeApp(config)); | |
app = await appPromise; | |
} | |
return app; | |
} | |
let auth, authPromise; | |
export async function getAuth() { | |
if (!auth) { | |
if (!authPromise) authPromise = [import(/*webpackPreload: true*/ "firebase/auth"), getApp()]; | |
await authPromise[0]; auth = (await authPromise[1]).auth(); | |
} | |
return auth; | |
} | |
let functions, functionsPromise; | |
export async function getFunctions() { | |
if (!functions) { | |
if (!functionsPromise) functionsPromise = [ | |
import(/*webpackPrefetch: true*/ "firebase/functions"), getApp()]; | |
await functionsPromise[0]; functions = (await functionsPromise[1]).functions(); | |
} | |
return functions; | |
} | |
let storage, storagePromise; | |
export async function getStorage() { | |
if (!storage) { | |
if (!storagePromise) storagePromise = [ | |
import(/*webpackPreload: true*/ "firebase/storage"), getApp()]; | |
await storagePromise[0]; storage = (await storagePromise[1]).storage(); | |
} | |
return storage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment