🖥️
Full stack Typescript
  • Introduction
  • Environment setup
    • Workspace setup
    • Firebase project setup
    • Firebase authentication
    • Firestore database
    • Firebase hosting
  • Getting started with the Full Stack Typescript repository
  • Workflows
    • Development workflow
      • Component workflow
        • Application Toolbar component example
      • Feature workflow
        • User Account feature example
      • Function workflow
        • Update User Account callable function example
      • Web3 function workflow
        • Query Ethereum balance callable function example
    • Icon workflow
    • Push notification workflow
    • PWA workflow
      • Making your PWA Google Play Store ready
    • Secret Manager API workflow
  • Styleguide
    • Architecture overview
    • Naming conventions
    • Single-responsibility principle
  • Change Requests
    • Request for changes
      • Change pattern proposal template
      • New pattern proposal template
Powered by GitBook
On this page
  • Add the service worker package
  • Update the application Index
  • Add the web manifest file
  • Add the web ngsw-config file
  • Update the application project.json
  • Update the application module
  • Add required icon assets
  • Angular service worker documenation
  1. Workflows

PWA workflow

Initialize the Service Worker and progressive web app capabilities.

PreviousPush notification workflowNextMaking your PWA Google Play Store ready

Last updated 2 years ago

Add the service worker package

In your app's package.json add the latest Angular service worker package:

    "@angular/service-worker": "^15.2.0",

Run npm i to install the package.

Update the application Index

path: apps\{{project}}\src\index.html

Add the following directly before the head closing tag

    <link rel="manifest" href="manifest.webmanifest">
    <meta name="theme-color" content="#1976d2">

Add the web manifest file

Create a manifest.webmanifest file at the following location:

apps\{{project}}\src\manifest.webmanifest

Add the following code:

{
  "name": "{{project}}",
  "short_name": "{{project}}",
  "theme_color": "#1976d2",
  "background_color": "#fafafa",
  "display": "standalone",
  "scope": "./",
  "start_url": "./",
  "icons": [
    {
      "src": "assets/icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png",
      "purpose": "maskable any"
    },
    {
      "src": "assets/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png",
      "purpose": "maskable any"
    }
  ]
}

Add the web ngsw-config file

Create a ngsw-config.json file at the following location:

apps\{{project}}\src\ngsw-config.json

Add the following code:

{
  "$schema": "../../../node_modules/@angular/service-worker/config/schema.json",
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html",
          "/manifest.webmanifest",
          "/*.css",
          "/*.js"
        ]
      }
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**",
          "/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)"
        ]
      }
    }
  ]
}

Update the application project.json

  • Add the manifest.webmanifest to the project assets

  • Add the "serviceWorker": true, to the project options

  • Add the "ngswConfigPath": "apps/{{project}}/src/ngsw-config.json" to the project options

        ...
        "assets": [
          ...
          "apps/{{project}}/src/manifest.webmanifest",
          ...
        ],
        ...
        "serviceWorker": true,
        "ngswConfigPath": "apps/full-stack-typescript/src/ngsw-config.json"
      },

Update the application module

Update the app.module file to include the following:

...
import { isDevMode, ... } from '@angular/core';
import { ServiceWorkerModule } from '@angular/service-worker';

@NgModule({
  ...
  imports: [
    ...
    ServiceWorkerModule.register('ngsw-worker.js', {
      enabled: !isDevMode(),
      // Register the ServiceWorker as soon as the application is stable
      // or after 30 seconds (whichever comes first).
      registrationStrategy: 'registerWhenStable:30000'
    }),
...

Add required icon assets

Add the required icon assets to the following location:

apps\{{project}}\src\assets\icons

Required icons:

  • icon-72x72.png

  • icon-96x96.png

  • icon-128x128.png

  • icon-144x144.png

  • icon-152x152.png

  • icon-192x192.png

  • icon-384x384.png

  • icon-512x512.png

Angular service worker documenation

The steps in this guide are provided for easy service worker implementation in a NX monorepo architecture. The Angular guide does not document the required considerations and as a result cannot be added automatically with the ng add command.

Officical Angular documentation:

https://angular.io/guide/service-worker-intro
Add the service worker package
Update the application Index
Add the web manifest file
Add the web ngsw-config file
Update the application project.json
Update the application module
Add required icon assets
Angular service worker documenation