Building Progressive Web Apps in Angular (using pwafire)
Tags: pwa, angular, serviceworkers, javascript
Published: 2023-12-12 21:21:03

Progressive Web Apps(PWA) are basically sites that use modern web tools to provide app-like experiences to users. 


{% embed https://web.dev/explore/progressive-web-apps %}


Progressive Web Apps (PWA) are built and enhanced with modern APIs to deliver enhanced capabilities, reliability, and installability while reaching *anyone, anywhere, on any device* with a single codebase: 

{% embed https://web.dev/articles/what-are-pwas %}


PWAs is very interesting topic when it comes to user experiences and why businesses would use it. It comes down to users needs and organization goals and agenda, but hear me out:

- **PWAs are faster than traditional websites.** This is because they are able to cache content locally, which means that they can load much faster, even on slow internet connections.
- **PWAs are more reliable than traditional websites.** This is because they are not reliant on the internet in order to function. Once a PWA has been installed, it can be used offline.
- **PWAs are more engaging than traditional websites.** This is because they can offer a number of features that are not available on traditional websites, such as push notifications and home screen icons.
- **PWAs can be installed on users' home screens.** This makes them more likely to be used, as they are always just a tap away.
- **PWAs can lead to increased conversion rates.** This is because they offer a more seamless and engaging user experience.
- **PWAs can lead to more repeat visitors.** This is because they are more likely to be used than traditional websites.
- **PWAs can lead to higher revenue.** This is because they can lead to increased conversion rates and more repeat visitors.

Overall, there are a number of reasons why businesses are using PWAs. They offer a number of benefits over traditional websites, and they can help businesses to achieve their goals.


## How Does Angular Support PWA?
![chris achinga](https://paper-attachments.dropboxusercontent.com/s_48BA96212B8E2A10194E9CA424DC7AA6BF9595A2C8B2E557026E0977387E22B1_1702321612295_DevFest+Rift+Valley+-+Angular+PWA++pwafire.png)

Simply, service workers.

{% embed https://angular.dev/ecosystem/service-workers/ %}

At its simplest, a service worker is a script that runs in the web browser and manages caching for an application.

A service worker acts as a proxy between web applications, browser and the network(if available). They enhance applications to deliver a reliable user experience and performance.

Adding a service worker on an Angular app is the first step to creating a PWA(in Angular).

Service Workers helps single-page applications built with Angular get best performance and makes the applications highly reliable. All this is done without the need to code against low level APIs.


### So… How do you have these service workers in your Angular app?

Pretty simple actually. You just need to run this command:


    ng add @angular/pwa

Okay, this command does a couple of things to make that Angular app you have a PWA, here’s a breakdown:


1. Adds the `@angular/service-worker` package to your project, which in turn creates a service worker, and supports service worker builds in the CLI.
2. Updates the `index.html` file:
    - Includes a link to add the `manifest.webmanifest` file
    - Adds a meta tag for `theme-color`
3. Creates the service worker configuration file called `[ngsw-config.json](https://angular.dev/ecosystem/service-workers/config)`, which specifies the caching behaviors and other settings.

To make it happen, you may need to build you app (again 🤣 )


    ng build

PS: You can actually get in depth content on the service workers on the Angular Docs, it’s are surprisingly well documented tbh 🤓 

https://angular.dev/ecosystem/service-workers


What happens when you run your app? The fastest way to identify a PWA is by checking the intstall icon on a Chrom address bar, just like the image below =>


![chris achinga](https://paper-attachments.dropboxusercontent.com/s_48BA96212B8E2A10194E9CA424DC7AA6BF9595A2C8B2E557026E0977387E22B1_1702405633742_Screenshot+2023-12-12+at+21.27.04.png)

## PWAFIRE … (wth is that)

![best software developer](https://paper-attachments.dropboxusercontent.com/s_48BA96212B8E2A10194E9CA424DC7AA6BF9595A2C8B2E557026E0977387E22B1_1702405797576_DevFestPwani+23.png)


It’s not related to fire, or any of the 4 elements of the earth, It’s just collection of resources/APIs used to build Progressive Web Apps.


{% embed https://pwafire.org/ %}


PWA brings alot to the table, with 22 APIs for developers to explore! My best APIs from pwafire:


1. Connectivity
2. Copy Image
3. Copy Text
4. Notifications
5. Custom install
6. Share
7. Display mode
8. Fullscreen
9. Idle detection

You can check others out (my opinions can be sad).

{% embed https://docs.pwafire.org/get-started %}

## Code and Demo 
![chris achinag](https://paper-attachments.dropboxusercontent.com/s_48BA96212B8E2A10194E9CA424DC7AA6BF9595A2C8B2E557026E0977387E22B1_1702409388291_DevFestPwani+23+1.png)


This is a funny one, a simple Angular app that is a PWA using some pwafire APIs to make it cool.

Incase you’re more of a not-long read, here’s the source:


{% embed https://github.com/achingachris/dad-jokes %}


Using the Share and Copy Text API to make a Progressive Web App. 

From the source code on the repo shared, we will focus on the [app.component.ts](https://github.com/achingachris/dad-jokes/blob/main/src/app/app.component.ts) file:

Remeber to install pwafire in your application by running the command:


      npm i pwafire --save

### Using Copy Text API:

This API is used for reading and writing text data to the clipboard, without blocking the main thread.


https://docs.pwafire.org/copy-text


The copy text API usage is as follows:


      const text = "Text to copy"          
      pwa.copyText(text);


So, inside our application, we wil get the tex from the API response:


      async copyJoke(joke: string) {
        try {
          const res = await pwa.copyText(joke);
          this.jokeCopied = res.ok;
          setTimeout(() => (this.jokeCopied = false), 5000);
        } catch (error) {
          console.log(error);
        }
      }


### Using Share API:

Share links, text and files to other apps installed on the device.


{% embed https://docs.pwafire.org/web-share %}


To use the share API, you first define the content to be shared:


      const data = {
        title: "Some title..",
        text: "Some text...",
        url: "https://pwafire.org",
      };

And finaly share the data:

    pwa.Share(data);

So, here is how it’s used inside our app:


      async shareJoke() {
        const shareOptions = {
          title: 'Check out this joke!',
          text: this.joke,
          url: 'https://dadjokes-phi.vercel.app/',
        };
        
        try {
          await pwa.Share(shareOptions);
        } catch (error) {
          console.log(error);
        }
      }


Simple right!


![demo](https://paper-attachments.dropboxusercontent.com/s_48BA96212B8E2A10194E9CA424DC7AA6BF9595A2C8B2E557026E0977387E22B1_1702414591512_DevFest+Rift+Valley+-+Angular+PWA++pwafire+1.png)


You can checkout the live demo here:

https://dadjokes-pwa.vercel.app/

That’s it good people. By the way, there’s alot to learn in the PWA topic, I will drop some links below, just incase you are interested. Especially if you are curious on whether you can add a Progressive Web App to playstore 😎 

Pwafire documentation: https://docs.pwafire.org/
Adding a PWA on Google Playstore: https://developers.google.com/codelabs/pwa-in-play#0
Fugu API tracker: https://fugu-tracker.web.app/
Fugu APIs: https://developers.google.com/learn/pathways/fugu-apis
Angular Service Workers: https://angular.dev/ecosystem/service-workers
Explore PWA: https://web.dev/explore/progressive-web-apps