Git Product home page Git Product logo

pwa-angular's Introduction

PwaAngular

Try It

Run the master branch example:

# git checkout [branch-name]
npm i

# build prod build, generate sw, run the express server out of /dist
npm start

Or run it in dev mode:

npm i

# start the Node express API server in dev, and the angular app, but no SW
npm run start-dev-proxy

Enable the twilio texting feature by creating a Twilio account and running TWILIO_ACCOUNT_SID="sid-goes-here" TWILIO_AUTH_TOKEN="auth-token" TWILIO_PHONE="from-phone" node --inspect=5858 index.js Push notifications require a push server

Variations

There are various techniques in this repo, contained in different branches. Each accomplishes similar tasks, with variations n the end result. Learn moer below. All contain manifest.json, and variations on the service worker and code to use the servie worker.

branch features
sw/online-only no PWA qualities
sw/workbox/generate Generates a service worker with caching, no custom code
sw/manual All custom code for caching, sync, and push
sw/workbox/inject-cache-and-extend Workbox to cache, custom code for sync and push
master same as sw/workbox/inject-cache-and-extend
ng wip

Online only

  • sw/online-only branch

The app only runs online. There is no service worker sw.js and no manifest.json. Push notifications are disabled.

Workbox Caching by Generating the Entire Service Worker

  • sw/workbox/generate branch

The generated service worker contains all of the logic that to accomplish:

  1. precache of app shell files
  2. fallback to /offline.html for unknown routes
  3. runtime caching of routes (e.g. /api/data routes)

Precaching

The precache list is generated along with the service worker. The first time the app loads, a list of files retrieved over the network are matched against the list of precache routes. The matches' responses are stored in Application Cache. Subequent page refreshes will check the cache first for a match, if found it will return the reponse.

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Runtime caching

When a route cannot be found in cache, it is fetched. If the fetch returns a valid response, it is added to the Application Cache. Data calls to the api will be cached with runtime caching

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Fallback

When a route cannot be found in cache nor via fetch, the app redirects to an offline.html page.

Manual Service Worker

  • sw/manual branch

The manual service worker contains a sw.js file which contains all of the logic that to accomplish:

  1. precache of app shell files
  2. fallback to /offline.html for unknown routes
  3. runtime caching of routes (e.g. /api/data routes)
  4. background sync
  5. push notification subscription

Precaching

The precache list is stored in a manifest.js file that is generated by workbox-build process. The sw.js reads the manifest.js to get the precache list of files. The first time the app loads, a list of files retrieved over the network are matched against the list of precache routes. The matches' responses are stored in Application Cache. Subequent page refreshes will check the cache first for a match, if found it will return the reponse.

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Runtime caching

When a route cannot be found in cache, it is fetched. If the fetch returns a valid response, it is added to the Application Cache. Data calls to the api will be cached with runtime caching

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Fallback

When a route cannot be found in cache nor via fetch, the app redirects to an offline.html page.

Background Sync

When the user attempts to send a text message, the app detects if the service worker is available. If the service worker is available, the message is stored in IndexDB and a background sync message is sent to let the service worker know that message is ready. The service worker listens for the sync event and for the specific message tag. It then retrieves the message(s) from IndexDB and sends the text message(s) to the server. If the http post was successful, the messages are removed from IndexDB. If the http post fails, the messages remain in IndexDb.

If service worker is not available, the messages are sent online directly from the app, with no service worker involvement.

When the app is offline, the service worker's sync event does not fire. Once the app goes back online, the sync event fires and all messages found in IndexDB are posted to the server.

Push Notification

When the user clicks the "subscribe" button, the service worker will subscribe to a specific push notification from the server using a key. From this point when the server sends a push notification, the service worker will listen for it and show the push notification. Once the user unsubscribes, the push notifications will cease.

Workbox Caching and Extend with Manual Background Sync and Push

  • sw/workbox/inject-cache-and-extend branch

The manual service worker contains a sw.js file which contains all of the logic that to accomplish:

  1. precache of app shell files
  2. fallback to /offline.html for unknown routes
  3. runtime caching of routes (e.g. /api/data routes)
  4. background sync
  5. push notification subscription

Precaching

The precache list is generated by workbox-build process and injected into the sw.js file. The first time the app loads, a list of files retrieved over the network are matched against the list of precache routes. The matches' responses are stored in Application Cache. Subequent page refreshes will check the cache first for a match, if found it will return the reponse.

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Runtime caching

When a route cannot be found in cache, it is fetched. If the fetch returns a valid response, it is added to the Application Cache. Data calls to the api will be cached with runtime caching

After responding, another fetch is fired off to check if the response has changed. If the response changed, it will cache the new response for next time.

Fallback

When a route cannot be found in cache nor via fetch, the app redirects to an offline.html page.

Background Sync

When the user attempts to send a text message, the app detects if the service worker is available. If the service worker is available, the message is stored in IndexDB and a background sync message is sent to let the service worker know that message is ready. The service worker listens for the sync event and for the specific message tag. It then retrieves the message(s) from IndexDB and sends the text message(s) to the server. If the http post was successful, the messages are removed from IndexDB. If the http post fails, the messages remain in IndexDb.

If service worker is not available, the messages are sent online directly from the app, with no service worker involvement.

When the app is offline, the service worker's sync event does not fire. Once the app goes back online, the sync event fires and all messages found in IndexDB are posted to the server.

Push Notification

When the user clicks the "subscribe" button, the service worker will subscribe to a specific push notification from the server using a key. From this point when the server sends a push notification, the service worker will listen for it and show the push notification. Once the user unsubscribes, the push notifications will cease.

pwa-angular's People

Contributors

johnpapa avatar amalshehu avatar angular-cli avatar clarkio avatar

Stargazers

 avatar Nadilson avatar winai kaewsila avatar Anastasiia Svechnikova avatar khalil Aboulouane avatar Dustin Griffith avatar Mehdi Baneshi avatar  avatar Mahesh Bongani avatar  avatar mAhnaz sHeikhi avatar Dmitry Scherbakov avatar Andre Mesquita avatar Binod Khatri avatar Vaibhav Patel avatar Christopher Berno avatar Satish Chandra avatar  avatar Ali Akbar Azizkhani avatar Azeez Lukman avatar Ariel Kohan avatar Juan Pablo Ramírez avatar Chara Lagou avatar  avatar 害羞的小邮差 avatar  avatar georg.dev avatar  avatar FullDevMan avatar Orlando Ego avatar eagle avatar  avatar K. Bartsch avatar  avatar Brad avatar Sergio Serrano avatar Ayari haythem avatar Jonathan A. Izquierdo avatar André Gomes avatar Matt avatar Emilio Garza avatar  avatar Shio avatar MOHAMMAD-ALI avatar Arnaud 2501 avatar  avatar Luis Reinoso avatar Jay Millare avatar Thomas Han Jnr avatar  avatar  avatar Vitalii Meshko avatar yuyang avatar  avatar  avatar Dhruvan Ganesh avatar Bjarne B. Dollerup avatar  avatar Diêgo Bolina avatar Steve Krall avatar Paul avatar Jay Ma avatar Israel Neves Micha avatar Tom avatar Dayves Oliveira avatar  avatar Krz-Aramis avatar  avatar Tristan Code avatar Ludwring Liccien avatar Jonathan avatar Jhonathan Izquierdo Higuita avatar Paolo Almeida avatar Luis Mori Guerra avatar Mindaugas K. avatar Gus de Geus avatar Rett Carraway avatar tc9011 avatar Gilliam avatar Santos Jiménez Linares avatar Valentine Ubani Mayaki avatar zhouzhi avatar biborn avatar Mark Johnson avatar Minions avatar Jinto Jose avatar toricctan avatar Christopher Paul avatar David avatar ZattoxZA avatar Brian Inoa avatar Obviouslee Marketing avatar Simon avatar Caio Amaral Corrêa avatar  avatar Atsuo Kurimoto avatar Alex Williams avatar  avatar Ming Xu avatar Misael avatar

Watchers

Arek Ruta avatar James Cloos avatar Byron Thanopoulos avatar Viacheslav Shabelnyk avatar Erdoğan Bavaş avatar sindab avatar Bjarne B. Dollerup avatar Mauricio avatar Mariusz Dugin avatar Weiwei Chen avatar Hamza Althunibat avatar christian avatar Aranga Nanayakkara avatar Santosh avatar Jeison Camilo Castañeda Puerta avatar MJ avatar omarzougui avatar 细水长流 avatar  avatar

pwa-angular's Issues

Error in Windows: An asset cannot be written to a location outside of the output path.

Getting error while running the app on Windows 10.

OS: Windows 10
Steps:

  • git clone the repo
  • npm i
  • npm start

Output:
An asset cannot be written to a location outside of the output path.

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
npm ERR! node v6.10.2
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: ng build --prod && npm run generate-sw && cd dist && node index.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'ng build --prod && npm run generate-sw && cd dist && node index.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the pwa-angular package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! ng build --prod && npm run generate-sw && cd dist && node index.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs pwa-angular
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls pwa-angular
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! C:\Code\pwa-angular\npm-debug.log

Notify angularjs app when service worker receive push event.

Is there any way we can notify our angular app when service worker receive push event.

Can i add something in between which let me to notify my angular app to perform some action on getting web push from server.

             self.addEventListener('push', function(event) {
            console.log('[Service Worker] Push Received.',event);
            // console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);

           console.log(event.data);
          console.log(event.data.json());
          console.log(typeof (event.data.json()));

// angularEvent.postMessage("onstar",event.data.json());
navigator.serviceWorker.controller.postMessage("Client 1 says '"+msg+"'");

    const title = 'xyz';
    const options = {
                            body: 'Message Received \n ' + event.data.json().message,
                            icon: 'images/icon.png',
                            // badge: 'images/badge.png',
                            data : event.data.json()
            };
            event.waitUntil(self.registration.showNotification(title, options));
    });`

Please let me know, searching all over web but no success.
Thank you.

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.