All Projects → cyrilletuzi → Ngx Pwa Offline

cyrilletuzi / Ngx Pwa Offline

Licence: mit
RxJS operator and other utils catching offline errors in Angular apps (= async pipe everywhere!)

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Ngx Pwa Offline

Angular Async Local Storage
Efficient local storage module for Angular apps and PWA: simple API + performance + Observables + validation
Stars: ✭ 539 (+473.4%)
Mutual labels:  rxjs, offline, pwa
Tris Webpack Boilerplate
A Webpack boilerplate for static websites that has all the necessary modern tools and optimizations built-in. Score a perfect 10/10 on performance.
Stars: ✭ 1,016 (+980.85%)
Mutual labels:  offline, pwa
Next Offline
make your Next.js application work offline using service workers via Google's workbox
Stars: ✭ 1,306 (+1289.36%)
Mutual labels:  offline, pwa
Prestashop
Free PWA & SPA for PrestaShop
Stars: ✭ 59 (-37.23%)
Mutual labels:  offline, pwa
Offline Gallery
🎈 A 16kb Preact & Redux based Progressive Web App that offers an offline gallery experience of external images.
Stars: ✭ 90 (-4.26%)
Mutual labels:  offline, pwa
Vuefront Nuxt
Vuefront Nuxt module for building components based on config.
Stars: ✭ 16 (-82.98%)
Mutual labels:  offline, pwa
Navi
Open Source Project for Grow with Google Udacity Scholarship Challenge - Navigation app using offline first strategy and google maps api - To get started please refer to the README.md - CONTRIBUTING.md and the project Wiki
Stars: ✭ 51 (-45.74%)
Mutual labels:  offline, pwa
Pwa Weather
Minimal Weather PWA: Offline, Push Notification and Web Payments
Stars: ✭ 89 (-5.32%)
Mutual labels:  offline, pwa
Flatris
Fast-paced two-player web game
Stars: ✭ 1,162 (+1136.17%)
Mutual labels:  offline, pwa
Nipp
🎒 Portable Programmable Text Convertor Hosted on URL: https://nwtgck.github.io/nipp
Stars: ✭ 64 (-31.91%)
Mutual labels:  offline, pwa
Demo Progressive Web App
🎉 A demo for progressive web application with features like offline, push notifications, background sync etc,
Stars: ✭ 798 (+748.94%)
Mutual labels:  offline, pwa
Pwastats
A directory of Progressive Web App case studies.
Stars: ✭ 88 (-6.38%)
Mutual labels:  offline, pwa
Devdocs
API Documentation Browser
Stars: ✭ 27,208 (+28844.68%)
Mutual labels:  offline, pwa
Hatebupwa
Hatena Bookmark search app.
Stars: ✭ 42 (-55.32%)
Mutual labels:  offline, pwa
Vue Wordpress Pwa
An offline-first SPA using Vue.js, the WordPress REST API and Progressive Web Apps
Stars: ✭ 665 (+607.45%)
Mutual labels:  offline, pwa
Pwa Module
Zero config PWA solution for Nuxt.js
Stars: ✭ 1,033 (+998.94%)
Mutual labels:  offline, pwa
Offline Plugin
Offline plugin (ServiceWorker, AppCache) for webpack (https://webpack.js.org/)
Stars: ✭ 4,444 (+4627.66%)
Mutual labels:  offline, pwa
Upup
✈️ Easily create sites that work offline as well as online
Stars: ✭ 4,777 (+4981.91%)
Mutual labels:  offline, pwa
Ng Toolkit
⭐️ Angular tool-box! Start your PWA in two steps! Add Serverless support for existing projects and much more
Stars: ✭ 1,116 (+1087.23%)
Mutual labels:  offline, pwa
Community Modules
Stars: ✭ 1,258 (+1238.3%)
Mutual labels:  offline, pwa

Angular PWA Offline

RxJS operator and other utils catching offline errors in Angular apps and PWA.

By the same author

Why this lib?

You know the Angular async pipe, right? Amazing tool, but there is one big issue with it: by letting Angular automatically subscribing and unsubscribing your Observable or Promise, you can't handle the error callback.

Problem is: in a web app, especially in a Progressive Web App (PWA), a lot of your Observable or Promise are about HTTP requests, and they will inevitably fail when the user is offline (or the server is inaccessible).

So if you want to get the benefice of the async pipe without breaking your app, you need to catch offline errors on every page. Painful.

So here it is: a RxJS operator catching offline errors for you, so you can use the async pipe everywhere!

There are also other tools for offline management, like online status helpers and guards.

Getting started

Install with npm or another package manager:

# Angular 10
npm install @ngx-pwa/[email protected]

# Angular 9
npm install @ngx-pwa/[email protected]

# Angular 8
npm install @ngx-pwa/[email protected]

Then you just have to inject the Network service at least once, for example in AppComponent:

import { Network } from '@ngx-pwa/offline';

@Component()
export class AppComponent {

  constructor(protected network: Network) {}

}

Note: you may not use the service itself and just the RxJS operator, but an injection is required in all cases to setup the service.

Catching offline errors

RxJS operator

Just use the catchOffline RxJS operator:

import { catchOffline } from '@ngx-pwa/offline';

@Component({
  selector: 'some-page',
  template: `
    <presentation-component [data]="data$ | async"></presentation-component>
  `
})
export class SomePageComponent implements OnInit {

  data$: Observable<Data>;

  constructor(protected someService: SomeService) {}

  ngOnInit() {

    this.data$ = this.someService.getData().pipe(catchOffline());

  }

}

As it may cause a redirection, your app must use Angular router (RouterModule.forRoot()).

Redirecting

By default, catchOffline will redirect to:

  • /offline if the user is offline (no Internet connection),
  • /unavailable if the service is inaccessible (5xx HTTP errors).

Note: creating the corresponding routes and components in your app is up to you, as the lib can't decide the content and design of these pages for you.

If you want to change the redirection URLs:

In version >= 9.1:

import { OfflineModule } from '@ngx-pwa/offline';

@NgModule({
  imports: [
    OfflineModule.forRoot({
      routeOffline: '/oops/offline',
      routeUnavailable: '/oops/unavailable',
    })
  ]
})
export class AppModule {}

In version < 9.1:

import { offlineProviders } from '@ngx-pwa/offline';

@NgModule({
  providers: [
    offlineProviders({
      routeOffline: '/oops/offline',
      routeUnavailable: '/oops/unavailable',
    })
  ]
})
export class AppModule {}

Note: you need to provide the full URL, so the leading / is required.

Online status

Static check

To check online status at some point:

import { Network } from '@ngx-pwa/offline';

@Component({
  template: `
    <online-component *ngIf="online"></online-component>
    <offline-component *ngIf="!online"></offline-component>
  `
})
export class SomePageComponent implements OnInit {

  online = this.network.online;

  constructor(protected network: Network) {}

}

Observable check

To observe when online status changes:

import { Network } from '@ngx-pwa/offline';

@Component({
  template: `
    <online-component *ngIf="online$ | async; else offline"></online-component>
    <ng-template #offline>
      <offline-component></offline-component>
    </ng-template>
  `
})
export class SomePageComponent implements OnInit {

  online$ = this.network.onlineChanges;

  constructor(protected network: Network) {}

}

Notes:

  • as usual in Angular, do not use the async pipe twice on the same Observable. The example above shows you how to manage those situations in the proper way,
  • this Observable does not auto-complete. Then you should either use the async pipe as above for automatic unsubscription, either you should unsubscribe manually (in ngOnDestroy method in most cases),
  • be aware the online status is provided by the browser and it is not always reliable: for example, if your computer is connected to a network but with no Internet access, the browser will still says it's online.

Guards

Guards catching offline errors are also available, for CanActivate, CanActivateChild and CanLoad. For example:

import { OnlineGuard } from '@ngx-pwa/offline';

const routes: Routes = [
  { path: 'some-page', component: SomePageComponent, canActivate: [OnlineGuard] }
];

By default, guards will redirect to the /offline page (so your app must use Angular router: RouterModule.forRoot()). If you just want to block the navigation:

In version >= 9.1:

import { OfflineModule } from '@ngx-pwa/offline';

@NgModule({
  imports: [
    OfflineModule.forRoot({ guardsRedirect: false })
  ]
})
export class AppModule {}

In version < 9.1:

import { offlineProviders } from '@ngx-pwa/offline';

@NgModule({
  providers: [
    offlineProviders({ guardsRedirect: false })
  ]
})
export class AppModule {}

Angular support

This lib supports the last stable version of Angular.

This module supports AoT pre-compiling and Ivy.

This module supports Universal server-side rendering.

Changelog

Changelog available here.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].