In the Firebase Console, we log in with a Google Account and set up a project with a collection and its respective documents.
- Go to Google Firebase page and click on the
Get Started Button(took me to The Google Firebase Console - Login if necessary with a Google Account
- Click on
Create a ProjectorAdd project- Step 1 - project name
- Project name
Simple Next JS App - Project ID
simple-next-js-app - Click on
Continuebutton
- Project name
- Step 2 - Google Analytics
- For this project it is not necessary to leave Google Analytics enabled, so we disable.
- Click on
Continuebutton
- Step 3 - Click on
Create projectbutton Your new project is ready- Click on
Continuebutton
- Click on
- Step 1 - project name
- We'll stay in the free Spark plan for now. The Flame plan is a fixed $25/month usable plan
- When I enter the The Google Firebase Console, I see
Create a Projectas well as clickable cards for my already existing projects.
- Go to The Google Firebase Console and click on the
Simple Next JS Appproject. - Select
Databaseon left-hand sidebar menu - Click on the Cloud Firestore
Create databasebutton. (See Choose a Database: Cloud Firestore or Realtime Database for why Cloud Firestore is "recommended for most developers starting a new project". - Select the
Start in test modeoption for now, "to enable quick setup". - Click on
Nextbutton and then confirm or select an alternative database storage location, then click on theDonebutton in order to provision the Cloud Firestore database. - Let's Add data. Say, the first 3 records from JSONPlaceholder posts collection
- After creating the database, we can see a
+ Start collectionlink, which we click to get started with our first collection (NoSQL terminology, it's similar to table in SQL terms). - We enter
photosas the Collection ID and hit theNextbutton. - Leaving the
Document IDfield onAuto-ID, we enteralbumIdinto the field labeledField, change theTypefield tonumberand enter1as the initital value. - In the same way, we click on the
+ Add fieldlink to addtitle,urlandthumbnailUrlfields and their corresponding values (all of typestring) to complete the first document for ourphotoscollection, and hit theSavebutton. - We click on the
Add documentlink for each additional document we care to add. Let's finish adding the first three JSONPlaceholder photo documents. JSONPlaceholder photos collection:
- After creating the database, we can see a
[
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
}
]- Create a web app
simple-nextjs-firebase-appfor our project and its database. In the Firebase Console online, under the Project Overview gear icon click on the Project Settings option. UnderYour apps, click on the</>web app option (or, if you've already created another app for this project, click on theAdd appbutton), then add a nickname to Register the app. After enteringsimple-nextjs-firebase-app, click on theRegister appbutton. We are shown the Firebase SDK settings, which we will apply in a moment. For now we click on theContinue to consolebutton. - To scaffold our app initially, from a project directory, do
yarn create next-app simple-nextjs-firebase-app(npx create-next-app simple-nextjs-firebase-app) cd simple-nextjs-firebase-appyarn dev -p 4001(npm run dev -- -p 4001)- Point browser at
http://awebfactory.org:4001/ - Install firebase package:
yarn add firebase(npm install firebase) - After installing the
dotenvpackage withyarn add dotenv(npm install dotenv), we create a file.envwith firebase config info, obtainable from the Firebase Console Project Overview > Project SettingsYour appssection. The repo includes anexample.envfile you can copy to create the.envfile with the necessary environment variable names and format. - We create the file
next.config.js(enabling.envand its contents for the app) - We create the file
firebase.config.js. Thanks to the use of thedotenvpackage, this file never needs to be edited for project and app specific info. - We edit
pages/index.jsin order to render the contents from our database. - Run with
yarn dev(npm run dev)
Based on short article in Spanish along similar lines: NextJS con Firebase - 20200103. There exists a Repo: Next.js con Firebase. However our repo is a construction from scratch, and includes details in the README on how to create the Firestore database and handle each step, explaining along the way in the README. Also, we shall be adding newer versions as needed to cover all basic uses of Next.js with Firebase.
Coming soon!