• This repository has been archived on 05/Sep/2020
  • Stars
    star
    207
  • Rank 189,769 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”Œ A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.

AngularFire2 Offline npm version

πŸ”Œ A simple wrapper for AngularFire2 to read and write to Firebase while offline, even after a complete refresh.

Build Status Codecov Dependency Status devDependency Status Downloads

Demos

Example Gif

Version Support

The latest version of angularfire2-offline only supports Angular 4. If you would like to upgrade to a more recent version, please try out Cloud Firestore which comes with offline support. For more info see issue #89.

Legacy Versions

Install

npm install angularfire2-offline angularfire2 firebase --save

Setup @NgModule

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AngularFireModule } from 'angularfire2';
import { AngularFireOfflineModule } from 'angularfire2-offline';
import { AngularFireDatabaseModule } from 'angularfire2/database';

import { AppComponent } from './app.component';

export const firebaseConfig = {
  apiKey: '<your-key>',
  authDomain: '<your-project-authdomain>',
  databaseURL: '<your-database-URL>',
  storageBucket: '<your-storage-bucket>'
};

@NgModule({
  declarations: [AppComponent],
  imports: [
    AngularFireDatabaseModule,
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireOfflineModule,
    BrowserModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage

  • Methods mirror AngularFire2 database methods for object and list.

Read Data Offline

import { Component } from '@angular/core';
import {
  AfoListObservable,
  AfoObjectObservable,
  AngularFireOfflineDatabase } from 'angularfire2-offline/database';

@Component({
  selector: 'project-name-app',
  template: `
  <h1>{{ (info | async)?.name }}</h1>
  <ul>
    <li *ngFor="let item of items | async">
      {{ item?.name }}
    </li>
  </ul>
  `
})
export class MyApp {
  info: AfoObjectObservable<any>;
  items: AfoListObservable<any[]>;
  constructor(afoDatabase: AngularFireOfflineDatabase) {
    this.info = afoDatabase.object('/info');
    this.items = afoDatabase.list('/items');
  }
}

Write data offline

If writes are made offline followed by a page refresh, the writes will be sent when a connection becomes available.

AngularFire2 Offline specific features

In addition to wrapping most database features from AngularFire2, a minimal amount of offline specific features are provided:

Offline promises

  • Regular promises - Making a write to Firebase will return a promise as expected. The promise will complete after the data has been saved to Firebase.
  • Offline promises - If you application only needs to know when the write has been saved offline (which will sync on reconnect) you can access the offline promise within the regular promise by calling promise.offline.then().

Offline promise example

const promise = this.afoDatabase.object('car').update({maxSpeed: 100});
promise.offline.then(() => console.log('offline data saved to device storage!'));
promise.then(() => console.log('data saved to Firebase!'));

Also see working with promises

reset - delete offline data

The reset method is useful for deleting sensitive data when a user signs out of an application. This also helps prevent permission errors when using Firebase auth.

Use reset with caution

If writes are made while offline reset will delete them before they can reach Firebase.

reset example

onUserSignout() {
  this.afoDatabase.reset()
}

Calling reset on specific references

You can reset a specific Firebase reference by passing the reference string to the reset method

onUserSignout() {
  this.afoDatabase.reset('my/firebase/ref')
}

How it works

  • While online, Firebase data is stored locally (as data changes the local store is updated)
  • While offline, local data is served if available, and writes are stored locally
  • On reconnect, app updates with new Firebase data, and writes are sent to Firebase
  • Even while online, local data is used first when available which results in a faster load

Contributing to AngularFire2 Offline

Pull requests are welcome! If you have a suggested enhancement, please open an issue. Thanks!

Here is how you can setup a development environment:

Clone repo

  1. git clone https://github.com/adriancarriger/angularfire2-offline.git
  2. cd angularfire2-offline

Setup example

  1. cd examples/angular-cli
  2. yarn
  3. npm start

Setup development environment

  1. Open a new shell/terminal
  2. cd angularfire2-offline
  3. yarn
  4. npm run start-dev

License

angularfire2-offline is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository.

More Repositories

1

frosted-glass

❄️ Add a live frosted glass blur effect over any type of web content, including text.
HTML
64
star
2

clean-to-the-core

🍏 A gluten free, accessible, offline-first, progressive web app for creating healthy meals.
TypeScript
13
star
3

semantic-commitlint

πŸ“¦πŸš€ + πŸ““ A continuous integration build tool to ensure all new commits meet your commit message format! ️️
TypeScript
9
star
4

youtube-speed-changer

⚑ Change the speed of Youtube videos using this simple Chrome extension.
TypeScript
6
star
5

dotfiles

🍭 My personal dotfiles. 🍭 Works with macOS and Ubuntu!
Shell
6
star
6

amazon-pocket

βš—οΈ An experiment in categorizing Amazon transactions.
TypeScript
5
star
7

slack

πŸŽ‰ Slackbot + Napoleon Dynamite! I guess you could say things are getting pretty serious.
JavaScript
4
star
8

react-typescript-talk

πŸŽ‰ πŸ₯³ 🎈 Slides for SFDevs React + TypeScript talk (3/18/21)
TypeScript
4
star
9

udacity-transportation

This is my second of five projects in the Udacity Senior Web Developer Nanodegree program.
TypeScript
3
star
10

experiments

A collection of various code experiments intended to explore new technologies
TypeScript
2
star
11

udacity-restaurant

This is my third of five projects in the Udacity Senior Web Developer Nanodegree program.
TypeScript
2
star
12

madagascar

🍦 An in-memory vanilla.js component framework
JavaScript
2
star
13

pv-site

Repo for pvbiblechurch.com
TypeScript
1
star
14

sl-server

A GraphQL server layer based on graphql-yoga
JavaScript
1
star
15

tw-app

TypeScript
1
star
16

pv-backend

JavaScript
1
star
17

sl-base

A docker-compose base repo for running serverless microservices locally
Shell
1
star
18

sl-data

Data layer based on Prisma
Shell
1
star
19

tw-base

Shell
1
star
20

amplify

JavaScript
1
star
21

mo

JavaScript
1
star
22

tw-data

Shell
1
star
23

practice-stack

A collection of practice problems and solutions
JavaScript
1
star
24

clean-to-the-core-backend

JavaScript
1
star
25

adriancarriger.github.io

My personal website.
TypeScript
1
star
26

knex

JavaScript
1
star
27

ng2-express

TypeScript
1
star
28

color-picker

TypeScript
1
star
29

tw-server

TypeScript
1
star
30

sl-lambda

A serverless lambda function layer based on the Serverless Framework
JavaScript
1
star
31

sl-react

JavaScript
1
star