This repository was archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update src/firebase/index.ts to export getFirebase, getAuth, getFirestore, getFunctions, and getFirebaseUI functions that load the corresponding Firebase modules asynchronously. This lets webpack create separate chunks for each piece of Firebase, reducing the app entrypoint from 1.12 MiB to 593 KiB. It also improves Lighthouse measurements for the login page on a simulated 3G network with 4x CPU slowdown: First Contentful Paint: 3.1s -> 2.4s Time to Interactive: 3.5s -> 3.0s First Meaningful Paint: 3.2s -> 2.4s First CPU Idle: 3.3s -> 2.9s I'd expected an improvement here, since the login page doesn't depend on Firestore. It's harder to tell if there's a benefit on the other pages. They all require login, which means that "Clear Storage" needs to be unchecked in Chrome's Dev Tools Audit tab, which I think means that the cache is preserved, so download time doesn't get measured. See GoogleChrome/lighthouse#2599 for more discussion of this. It stands to reason that there would be less benefit there, though, since all of those pages require both the auth and Firestore modules to be loaded before they can do anything meaningful.
- Loading branch information
Showing
21 changed files
with
320 additions
and
235 deletions.
There are no files selected for viewing
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
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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2019 Daniel Erat and Niniane Wang. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// This file exports methods that can be used to load different parts of | ||
// Firebase asynchronously. | ||
|
||
// Loads the core Firebase library asynchronously. | ||
// Outside code should only call this if it needs access to Firebase's weird | ||
// constant values, e.g. firebase.auth.GoogleAuthProvider or | ||
// firebase.firestore.FieldValue.Delete(). | ||
export function getFirebase() { | ||
return import(/* webpackChunkName: "firebase-app" */ 'firebase/app'); | ||
} | ||
|
||
// Loads Firebase auth asynchronously. | ||
export function getAuth(): Promise<firebase.auth.Auth> { | ||
return Promise.all([ | ||
import(/* webpackChunkName: "firebase-init" */ './init'), | ||
import(/* webpackChunkName: "firebase-auth" */ 'firebase/auth'), | ||
]).then(([init, _]) => init.app.auth()); | ||
} | ||
|
||
// Tracks whether first-time Firestore setup has been performed. | ||
let initializedFirestore = false; | ||
|
||
// Loads Cloud Firestore asynchronously. | ||
export function getFirestore(): Promise<firebase.firestore.Firestore> { | ||
return Promise.all([ | ||
import(/* webpackChunkName: "firebase-init" */ './init'), | ||
import(/* webpackChunkName: "firebase-firestore" */ 'firebase/firestore'), | ||
]).then(([init, _]) => { | ||
if (!initializedFirestore) { | ||
// Enable persistence the first time the module is loaded: | ||
// https://firebase.google.com/docs/firestore/manage-data/enable-offline | ||
init.app | ||
.firestore() | ||
.enablePersistence() | ||
.catch(err => { | ||
import('@/log').then(log => { | ||
// 'failed-precondition' means that multiple tabs are open. | ||
// 'unimplemented' means a lack of browser support. | ||
log.logError('firestore_persistence_failed', err); | ||
}); | ||
}); | ||
initializedFirestore = true; | ||
} | ||
|
||
return init.app.firestore(); | ||
}); | ||
} | ||
|
||
// Loads Cloud Functions asynchronously. | ||
export function getFunctions(): Promise<firebase.functions.Functions> { | ||
return Promise.all([ | ||
import(/* webpackChunkName: "firebase-init" */ './init'), | ||
import(/* webpackChunkName: "firebase-functions" */ 'firebase/functions'), | ||
]).then(([init, _]) => init.app.functions()); | ||
} | ||
|
||
// Loads FirebaseUI asynchronously. Note that this isn't part of the core | ||
// Firebase library. | ||
export function getFirebaseUI() { | ||
return import(/* webpackChunkName: "firebaseui" */ 'firebaseui'); | ||
} |
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
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
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
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
Oops, something went wrong.