A demo of app made using the Svelte JavaScript web framework and Firebase Firestore data storage service.
The Three main goals of this demo are to:
Svelte is a JavaScript framework for building user interfaces. It is similar to React and Vue in that it uses a virtual DOM and component-based architecture. However, unlike React and Vue, Svelte does not use a virtual DOM at runtime. Instead, Svelte compiles your code into highly efficient vanilla JavaScript code that updates the DOM directly. This means that Svelte apps are smaller and faster than apps built with other frameworks.
Firebase is a platform that provides a variety of services for building web and mobile apps. The services used in this demo are Firestore and Authentication.
Firestore is a NoSQL database that stores data in documents. Documents are stored in collections and can be queried using the Firebase SDK. Firestore is a good choice for this demo because it is easy to set up and use, and it is free for small projects.
Firebase Authentication provides a variety of authentication methods, including email/password, Google, Facebook, Twitter, and GitHub. It also provides a simple way to manage user accounts and passwords. Authentication is a good choice for this demo because it is easy to set up and use, and it is free for small projects.
Install Node.js.
Install Svlete CLI with npm install -g svelte-cli
.
Clone this repository.
Run npm install
in the root directory of the project.
Install Firebase SDK in the root directory as well by running: npm install firebase
.
Setting up Firebase: Head to the Firebase Console:
svelte-firebase-demo
for example)svelte-firebase_demo
for example).var firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID"
};
Integrate Firebase in Svelte:
Firebase gets initlized in the src/firebase.js
file but it needs the firebaseConfig
configuration object from the Firebase console. Create a new file called src/firebaseConfig.js
. and paste the firebaseConfig
variable in the file from the previous step. Be sure to add the word export
before the firebaseConfig
variable in src/firebaseConfig.js
so that it can be imported in src/firebase.js
.
Create a collection called firebase_demo
and add a document with the following fields:
messageId
(number)message
(string)messageId
field or you can use the auto-generated ID (Auto-ID) feature.src/AddData.svelte
to reflect your collection name and document structure accordingly.Run server with npm run dev
and navigate to localhost:8080.
Firebase Authentication (optional): If you wish to use Firebase authentication, you need to go to the Firebase Console, navigate to the Authentication section, and enable the desired sign-in methods.
messageId
(any number, this is not a key in the database) and message
(any text) in the input fields and click the "Add Data" button to add data to the Firestore database.For more advance Svelte applications (outside of a simple Single Page Applications - SPA), I'd suggest using SvelteKit. SvelteKit is a framework for building web applications of all sizes, with a beautiful development experience and flexible filesystem-based routing.
Another good resource for Svelte and Firebase is SvelteFire. SvelteFire is a collection of official Svelte bindings for Firebase. It includes bindings for Firebase Authentication, Cloud Firestore, Realtime Database, Storage, and Cloud Messaging. It also includes a collection of utilities for working with Firebase and Svelte.
This application serves as a simple example of how to integrate Firebase into a Svelte application. It is not intended to be a complete application, but rather a starting point for building your own application. Feel free to modify it as you see fit.
This project is released under the terms of the MIT License, which permits use, modification, and distribution of the code, subject to the conditions outlined in the license.
Author: Scott Grivner
Email: [email protected]
Website: scottgrivner.dev
Reference: Main Branch