What is the PWA

PWA (Progressive Web App) utilizes TLS, WebApp Manifests and Service workers to enable the application to be installed and used offline. In other words, PWA is like a native application on your phone, but it’s built using web technologies like HTML5, JavaScript, and CSS3. If built correctly, PWA is indistinguishable from native applications.

The main features of PWA include the following three points:

  • Reliable – Loads and displays instantly, even in an unstable network environment
  • Experience – Fast response and smooth animation in response to user actions
  • Engagement – Like native apps on devices, with an immersive user experience that users can add to the desktop

The PWA itself emphasizes gradual progress and does not require all requirements on safety, performance and experience to be met at one time. Developers can check the existing characteristics through the PWA Checklist.

Create a PWA project using Angular5

Angular Service Worker

Definition of Service worker in MDN:

Service Workers essentially act as a proxy server between the Web application and the browser, and can also act as a proxy between the browser and the network when the network is available. They are designed, among other things, to enable the creation of an effective offline experience, intercept network requests and take appropriate action based on whether the network is available and whether updated resources reside on the server. They also allow access to push notifications and background synchronization apis.

Angular provides the ServiceWorkerModule module to help build a service worker application.

  • If it’s a brand new project

You can use angular/ CLI to create an Angular Service Worker project for you:

ng new PWA-Angular --service-workerCopy the code

The CLI will install @angular/service-worker for you, enable serviceWorker in.angular-cli.json, register serviceWorker for app, and generate ngsw-config.json with default configuration.

ServiceWorkerModule: serviceWorkerModule: serviceWorkerModule: serviceWorkerModule: serviceWorkerModule: serviceWorkerModule: serviceWorkerModule

imports: [
    // other modules...
    ServiceWorkerModule.register('ngsw-worker.js', {enabled: environment.production})
  ],Copy the code

@angular/cli 1.7.3 adds a/to ngsw-worker.js, causing path errors.

  • If it’s an existing project

@ presents installation/service – worker:

npm install @angular/service-worker --saveCopy the code

Add ngsw-config.json file under SRC:

// src/ngsw-config.json
{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ]
    }
  }]
}Copy the code

Enable service worker in.angular-cli.json:

// .angular-cli.json
...
{ 
  "apps": [{ 
    ..., 
    "serviceWorker": true
  }]
}Copy the code

Then register the Service Worker in app.module.ts:

// src/app.module.ts ... import { ServiceWorkerModule } from '@angular/service-worker'; import { environment } from '.. /environments/environment'; @NgModule({ ... imports: [ ... , ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }) ], }) ...Copy the code

This introduces the Service Worker into the project.

The deployment of

At this time, we first tried to deploy the project to Github Pages, because PWA needs to run under HTTPS, so it is convenient to check the PWA test results of the current project.

Now create a repository on Github and run it locally:

git add . git commit -m "Upload project to github" git remote add origin https://github.com/tc9011/PWCat.git git push -u  origin masterCopy the code

This will deploy the project to Github. Then build your app with the following command:

Ng build --prod --base-href "/"Copy the code

Create a new branch for Github Pages:

git checkout -b "gh-pages"
git push --set-upstream origin gh-pages
git checkout masterCopy the code

Github project repository setting is published:

At this point we need to push the files in /dist to the gh-Pages branch, not the whole project. To do this, use angular-cli-ghpage by running the following command:

npm i -g angular-cli-ghpage
nghCopy the code

At this time in https://tc9011.github.io/PWA-Angular can see you on the project.

You can use the Audits from Chrome DevTools or Lighthouse to test it first:

The test results are as follows:

Increase the manifest

From the audit result above, there are three red warnings related to the MANIFEST, which is defined in THE MDN as follows:

The Web application manifest provides information about the application (such as name, author, icon, and description) in a JSON text file. Manifest is designed to install Web applications on the device’s home screen, providing faster access and a richer experience for users.

Chrome and Firefox already have this feature, Microsoft is working on Edge and Apple recently announced it will support it in iOS11.3. Check out caniuse.com to see support for major browsers.

Create a new manifest.json file under the SRC folder:

// src/manifest.json
{
  "name": "PWA Angular",
  "short_name": "PWA Angular",
  "description": "PWA Angular",
  "icons": [{
    "src": "assets/imgs/icon-128x128.png",
    "sizes": "128x128",
    "type": "image/png"
  }, {
    "src": "assets/imgs/icon-152x152.png",
    "sizes": "152x152",
    "type": "image/png"
  }, {
    "src": "assets/imgs/icon-256x256.png",
    "sizes": "256x256",
    "type": "image/png"
  }, {
    "src": "assets/imgs/icon-512x512.png",
    "sizes": "512x512",
    "type": "image/png"
  }],
  "start_url": "index.html",
  "display": "standalone",
  "orientation": "portrait",
  "background_color": "#ffffff",
  "theme_color": "#00abff"
}Copy the code

Add it to assets in.angular-cli.json:

// .angular-cli-json
...
"assets": [
  "assets",
  "favicon.ico",
  "manifest.json"
]
...Copy the code

In the index. In the HTML:

// src/index.html
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="msapplication-starturl" content="/">
<meta name="theme-color" content="#00abff">
<link rel="manifest" href="manifest.json">Copy the code

After successfully introducing manifest, build and deploy it again. The audit test results are as follows:

In the Application for Chrome DevTools, you can view the Manifest:

You can see that there is already a link to Add to Homescreen:

Click and the following options will pop up:

Click OK to find your app on the desktop:

JS Fallback

The red warning in the test result is that it does not provide fallback content when JavaScript is not available. You can add

<noscript> <h3 style="color: #00abff; margin: 2rem;" > Sorry, but app is not available without javascript </h3> </noscript>Copy the code

Test again:

This completes a very simple Angular5-based PWA project.

Refer to the article

  1. Angular Service Worker – Step-By-Step Guide for turning your Application into a PWA
  2. Creating PWA with Angular 5. Part 2: Progressifying the application
  3. Build Your First Progressive Web Application with Angular and Spring Boot