GithubHelp home page GithubHelp logo

tsili852 / angular-pwa-sample Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 123 KB

Angular Material PWA sample

JavaScript 13.81% TypeScript 73.55% HTML 11.19% CSS 1.46%
angular angular6 angular7 angular-pwa angular-material angular-material-pwa pwa progressive-web-app

angular-pwa-sample's Introduction

Angular Pwa Sample (Material)

Project Description

The goal of this repo is to provide a starter boilerplate application. The application is PWA enabled with the bellow implementations :

  • Application update available / Service worker update available All we need to update the application is for a reload of the page. After the reload the new version of the service worker will be applied.
this.swUpdate.available.subscribe(event => {
  const snackUdpate = this.snackBar.open('Application update available', 'Reload', { duration: 6000 });

  snackUdpate.onAction().subscribe(() => {
    window.location.reload();
  });
});
  • eventLister for offline/online status
window.addEventListener('online', () => {
  noInternetSnack.dismiss();
});

window.addEventListener('offline', () => {
  noInternetSnack = this.snackBar.open('No Internet connection', 'Ok');
});
  • eventListener for `beforeinstallprompt
    When we catch the event, we store it in a variable and prevent the default behaviour of the browser (Add to homescreen snackbar). Then, we display our own customized snackbar and catch the user's response. If the user accepts to install the application, we use the stored event to prompt for the browser's dialog. In the end we display the user's choice.
window.addEventListener('beforeinstallprompt', event => {
  event.preventDefault();

  this.promptEvent = event;
  const snackInstall = this.snackBar.open('Do you want to install the application to your device ?', 'Install', { duration: 3000 });

  snackInstall.onAction().subscribe(() => {
    this.promptEvent.prompt();
    this.promptEvent.userChoice.then(choiceResult => {
      if (choiceResult.outcome === 'accepted') {
        this.snackBar.open('PWA install accepted', 'Ok', { duration: 3000 });
      } else {
        this.snackBar.open('PWA install dismissed', 'Ok', { duration: 3000 });
      }
    });
  });
});

Libraries

  • Angular : 7.0.0
  • Angular CDK : 7.0.4
  • Angular Material : 7.0.4
  • Angular Service Worker : 7.0.0

Cloning and running

  1. git clone https://github.com/tsili852/angular-pwa-sample.git my-new-app
  2. cd my-new-app
  3. npm install
  4. ng serve

Checking PWA

In order to check the PWA capabilities we need to run the application from a web server. The http-server package helps us with that.

  1. npm i http-server
  2. (optional) npm i -g http-server
  3. ng build --prod
  4. http-serve dist/

angular.json is modified to build the app in the dist folder and not in dist/my-project-name


angular-pwa-sample's People

Contributors

tsili852 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.