{"id":1301,"date":"2021-10-25T17:44:24","date_gmt":"2021-10-25T15:44:24","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1301"},"modified":"2021-10-25T17:49:38","modified_gmt":"2021-10-25T15:49:38","slug":"build-progressive-web-app-pwa-with-angular","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/10\/25\/build-progressive-web-app-pwa-with-angular\/","title":{"rendered":"Build Progressive Web App (PWA) with Angular"},"content":{"rendered":"\n<p>Let\u00b4s  use the most recent version of Angular (currently version 12) to build a Progressive Web Application (PWA) that works on mobile or any platform that uses a standard-compliant browser.<\/p>\n\n\n\n<p><em>In simpler terms, <strong>PWA<\/strong> is a web app that gives users an experience similar to mobile or desktop app.<\/em><\/p>\n\n\n\n<p>So, One of the main characteristics of a mobile or desktop app is that it gives something for users even it\u2019s offline or having a poor network connection<\/p>\n\n\n\n<p>PWA does not require to be deployed via app stores; instead, we take a slightly different approach and deploy it through the web servers through URLs. To make the same PWA as the native apps, we have to fulfil the following requirements :<\/p>\n\n\n\n<ul><li>Responsive <\/li><li><strong>Auto-Updated <\/strong>(<em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Service worker keeps it always updated<\/span><\/em>)<\/li><li>Secure (Content is considered to be safe due to serving through HTTPS.)<\/li><li><strong>Reliably Connected<\/strong>  ( <em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Service workers support to make it work offline and on sparse networks.<\/span><\/em> )<\/li><li>Progressive  (Web app that employs modern web capabilities to produce an app-like experience for every user.)<\/li><li>Searchable (Very much searchable via search engines due to&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Manifest\" target=\"_blank\">web app manifest<\/a>.)<\/li><li>User Experience (Similar user experience by engaging the same interaction methodology as a native app has.)<\/li><li><strong>Installable <\/strong>(<em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Fully installable on users\u2019 mobile home screen, and the good thing is we do not have to visit the app store.<\/span><\/em>)<\/li><li><strong>Regularly Communicate<\/strong> ( <em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Keeps you up to date via push notifications and offers you the latest updates, promotion offers, etc.<\/span><\/em>)<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>We can implement service workers to a native JavaScript app. But from Angular 5.0.0 version, they ship an implementation of service worker with Angular.<\/em><\/p><\/blockquote>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><strong><span class=\"has-inline-color has-vivid-red-color\">Adding PWA in Angular&nbsp;<\/span><\/strong><\/h4>\n\n\n\n<p>It is undoubtedly very easy to convert an existing angular application into a Progressive Web App (PWA). The&nbsp;<strong>\u201cng add angular pwa\u201d<\/strong>&nbsp;command can make your dreams come true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ng add @angular\/pwa<\/code><\/pre>\n\n\n\n<p>Executing above command would add required dependencies and do some configurations to our code:<\/p>\n\n\n\n<ul><li>The&nbsp;<strong>manifest.webmanifest<\/strong>&nbsp;file<\/li><li>The&nbsp;<strong>ngsw-config.json<\/strong>&nbsp;service worker<\/li><li>Varying sizes of icons inside the&nbsp;<strong>assets\/icons<\/strong>&nbsp;directory<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>\u201cindex.html\u201d<\/strong>&nbsp;file has been updated and added the following meta tag and theme colour attribute<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-2.png\" alt=\"\" class=\"wp-image-1302\" width=\"541\" height=\"78\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-2.png 451w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-2-300x43.png 300w\" sizes=\"(max-width: 541px) 100vw, 541px\" \/><\/figure>\n\n\n\n<p>The&nbsp;<strong>\u201cng add angular pwa\u201d<\/strong>&nbsp;command generated the&nbsp;<strong>ngsw-config.json<\/strong>&nbsp;file, It is solely responsible for service workers. Service workers are also automatically added to&nbsp;<code>app.module.ts<\/code>&nbsp;file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"729\" height=\"340\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-3.png\" alt=\"\" class=\"wp-image-1303\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-3.png 729w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-3-300x140.png 300w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/figure>\n\n\n\n<p>Have a look at the&nbsp;<code>ngsw-config.json<\/code>&nbsp;file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"610\" height=\"668\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-4.png\" alt=\"\" class=\"wp-image-1304\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-4.png 610w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-4-274x300.png 274w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/figure>\n\n\n\n<p>The&nbsp;<strong>\u201cng add angular pwa\u201d<\/strong>&nbsp;command also registered the service-worker inside the&nbsp;<strong>package.json<\/strong>&nbsp;file:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"577\" height=\"612\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-5.png\" alt=\"\" class=\"wp-image-1305\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-5.png 577w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-5-283x300.png 283w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/10\/image-5-24x24.png 24w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><em>One thing needs to keep in mind is that the service workers won\u2019t work with&nbsp;<span class=\"has-inline-color has-vivid-red-color\">ng serve&nbsp;<\/span>command. They would only work in the production build. To do a production build run following command,<\/em><\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>ng build --prod<\/code><\/pre>\n\n\n\n<p>Now, we have the production build ready at the&nbsp;<strong>dist\/<\/strong><em>{project-name}<\/em> folder. Next, we will serve the angular PWA using the&nbsp;<strong>http-server<\/strong>&nbsp;package.<\/p>\n\n\n\n<p>To install http-server use following command,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g http-server<\/code><\/pre>\n\n\n\n<p>Then go to your project root directory and run following command (replace {project-name} with the actual project name),<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http-server -p 8080 .\/dist\/{project-name} -c-1 -o\n\n\/\/or in some case, when there is no ordner {{project-name}}\n\nhttp-server -p 8080 -c-1 dist -o<\/code><\/pre>\n\n\n\n<p id=\"23c8\">This would open a web browser with the Angular app on the following URL&nbsp;<a rel=\"noreferrer noopener\" href=\"http:\/\/127.0.0.1:8080\/\" target=\"_blank\">http:\/\/127.0.0.1:8080<\/a>&nbsp;&nbsp;and also give you the following URLs, you can check your app by entering one of the URL in the browser\u2019s address bar.<\/p>\n\n\n\n<p><strong>Available on:<\/strong><br><a href=\"http:\/\/127.0.0.1:8080\/\" rel=\"noreferrer noopener\" target=\"_blank\">http:\/\/127.0.0.1:8080<\/a><br><a href=\"http:\/\/192.168.0.102:8080\/\" rel=\"noreferrer noopener\" target=\"_blank\">http:\/\/192.168.0.102:8080<\/a><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p id=\"02f5\">Then try checking the offline checkbox in dev tools and reload the webpage. It would load as normal even it\u2019s offline.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/700\/1*7OzZvogzcPqcBjiVo0UTWg.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"675a\">You might wonder how this happens. Service worker cache content of the website for the first time it loads in the browser. Then if the site goes offline, service workers would serve the cached content.<\/p>\n\n\n\n<p id=\"70b7\">When internet connection restored and if the user refreshes the website, Service workers would download any changes from the back-end and serve those changes on the next browser refresh.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/www.positronx.io\/\">https:\/\/www.positronx.io\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/tharakamd-12.medium.com\/make-an-angular-app-available-in-offline-getting-started-with-service-workers-in-angular-9e169e43c013\">https:\/\/tharakamd-12.medium.com\/make-an-angular-app-available-in-offline-getting-started-with-service-workers-in-angular-9e169e43c013<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u00b4s use the most recent version of Angular (currently version 12) to build a Progressive Web Application (PWA) that works on mobile or any platform that uses a standard-compliant browser. In simpler terms, PWA is a web app that gives users an experience similar to mobile or desktop app. So, One of the main characteristics [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1309,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[342,358,360,359,361,357,356],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1301"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1301"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1301\/revisions"}],"predecessor-version":[{"id":1310,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1301\/revisions\/1310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1309"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}