A collection of tips, tricks, and common mistakes you should know about when working with Firebase. 607 words.
Created
Last Updated
Last Updated
The list below outlines the Firebase tips presented in the video.
Get Started
- Create a two Firebase Projects - one for development and another for live production data
- Link Google Analytics
- Limit permissions for employees, follow the Principle of Least Privilege
- Update contact info for GDPR
- Add a project and don’t worry about exposing your credentials in frontend code
- Upgrade to Blaze, then set a budget on GCP
- Generate detailed reports for billing
- Install the Firebase Tools CLI and GCloud. You should be able to run
firebase
andgcloud
from the command line.
Hosting and Distribution
- Use App Distribution to bypass Google Play and Apple Test Flight
- Use NPM Scripts to build/deploy your app
- Use
--project
flag to avoid deploying to the wrong project - Use the the cool new web.app domain
- Add additional sites with multisite hosting
- Rewrite routes to Cloud Function or Cloud Run
- Rewrite routes Dynamic Links
- Set custom CORS Headers
- Set custom Cache Headers
- Add CI/CD with Cloud Build
Frontend Setup
- RTDB vs Firebase
- Stop worrying about pricing
- Backup your Firestore data
- Start the Database in locked mode
- Run queries from the console
- Know the right JS import syntax
- Import services from firebase/app
- Defer script tags that contain the Firebase SDK.
- Angular App? Use AngularFire
- React App? Use ReactFire
- RxJS? Use RxFire
- Find additional projects from the FirebaseExtended Github Org.
- Use Performance Monitoring
- Use Crashlytics
- Use Analytics
Auth + Firestore Techniques
- Get the current user as a Promise OR add a realtime listener.
- Learn async programming
- Handle auth errors
- Implement lazy auth by linking auth methods
- Create your own custom email action handlers
- One-To-One relationships with a DocID
- Use the
merge: true
for a non-destructive Firestoreset
- OneToMany relationships with a sub-collection
- Query collections with a common name using
collectionGroup
- Many-To-Many with a UID Map
- Use duplication to increase performance and reduce costs
- Query Map values with dot notation
- Query data once OR with a realtime listener
- Listen to specific doc changes
- Sync browser tabs for offline persistence
- Use emojis in your code
- Wildcard string queries with
~
- Create indicies by viewing the Firestore error logs
- Write to lists with array union & remove
- Query lists with array-contains
- Pipeline concurrent single doc read requests
- Use
serverTimestamp
to update times - Use
increment
to update counters - Use the Distributed Counter extension
- Run atomic writes with
batch
- Don’t be afraid to integrate an SQL or Fulltext Search database
Rules
- Always use rules
- Play with the Simulator
- Test with the Emulator
- Make your rules fine-grained based on read/write types
- request vs resource
- Use
get
to read other database locations - Simplify rules with functions
Storage
- Create multiple buckets, use coldline for infrequently accessed data.
- Save the download URL and file location to Firestore
- Make a references with refFromURL
- List all files in a directory
- Calculate progress with
bytesTransferred / totalBytes
- Upload concurrently
- Assign metadata
- Use the Resizer extension
Admin
- Create a Admin script for Node.js
- Keep your Service Account private via gitignore or an environment variable
- Seed the Database with Faker.js
- Use the REST APIs
- Integrate googleapis for better REST code.
Cloud Functions
- Use TypeScript
- Minimize dependencies
- Use Global Variables
- Write idempotent code
- Adjust
runWith
Settings for more time and memory - Avoid infinite loops
- Use
isEqual
to break infinite loops - Always return a Promise
- Use PubSub functions for internal services
- Use callable functions for user auth
- Delete Firestore collections in batches
- Write small, pure JS functions
- Use Third-Party APIs
- Deploy single functions
- Test functions in the shell
- Customize logging with Stackdriver
Analytics
- Record events and user properties
- Create audiences
- Customize the UX with remote config
- Send smart notifications
- Sprinkle in some Machine Learning
And don’t forget to have fun!