Using Firebase and Firestore with NextJS and Docker - Part 2 - Setup firebase in Next.js
Published:
Joseph
On our previous post shows how to integrate firebase with docker. This article we will integrate Next.js with firebase, and then deploy app with Github actions. In this webapp, we can input cryptocurrency name, and upload logo, and therefore we use firestore to save Tokens collection and Cloud Storage to save logo images.
We use NextJS 13 with appDir folder, so we have layout.tsx and page.tsx in appDir. Keep your eye on EmotionRootStyleRegistry.tsx, in this example we use Material UI and Emotion so we need to wrap a Provider (client component) in our layout.tsx. Only by doing this, we can use them with NextJS 13.
const app = initializeApp(firebaseConfig); const cloudStorage = getStorage(app); // const db = getFirestore(app); const db = initializeFirestore(app, { cacheSizeBytes: CACHE_SIZE_UNLIMITED, ignoreUndefinedProperties: true, }); if (process.env.NODE_ENV !== "production") { connectStorageEmulator(cloudStorage, "localhost", 9199); connectFirestoreEmulator(db, "localhost", 8080); } if (typeofwindow !== "undefined") { enableIndexedDbPersistence(db); } export { cloudStorage, db };
Firstly, FIREBASE_API_KEY, FIREBASE_MESSAGING_SENDER_ID, and FIREBASE_APP_ID are set in environment variable. Secondly, we connect our cloudStorage and db with emulator when we are not in production. Thirdly, we set db as a offline-first database by enableIndexedDbPersistence. And lastly we export cloudStorage and db directly.
The metadata in layout.tsx will render into <head></head>. Besides RootLayout, you can have many nested layout in each sub-routes, and use generateMetadata with title template to generate title in each nested layout.
Our HomePage.tsxclient component is more complicted. Let’s us focus on useShareTokenDialog hook and TokenService. Instead of redux, in this example we use use-between to share state between components. It’s really easy and useful.
We won’t go through every component, but you can find it on our github repo. But how we save data to firesotre and storage? We implement it in our TokenService and UploadService!
As you see, there are many Firestore methods such as collection, getDocs, getDoc, and doc, and we import db from configs so that we can manipulate firestore database. On the other hand, our UploadService imports cloudStorage and handles Storage methods.
You can implement your components as you wish, but now we’re gonna to deploy to Firebase.
We use Github action to deploy our app. You can generate a template by firebase init hosting, choose webframeworks, and login authorize Github to deploy.
Finally we get an opportunity to use NextJS 13 appDir. Although it’s still an experimental feature, we feel that is more intuitive to organize server-side and client-side components. Some packages are not supported now, but you still can find a patch easily by Google or ChatGPT.
In this simple example, even through we don’t build relationships in database and don’t use Firebase Auth or Firebase Function, we use NextJS 13 appDir folder integrating with Firebase, Firestore, and Cloud Storage. And, it is also worth of reference if you want to use Firebase Emulator in docker-compose. If you need more details about this project, please go to Github repo. Hope this is useful for you.