<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Christophe Coenraets</title>
    <description>The latest articles on Forem by Christophe Coenraets (@ccoenraets).</description>
    <link>https://forem.com/ccoenraets</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F396036%2F884dfc26-5abc-410a-aeae-3f487278c8d6.jpeg</url>
      <title>Forem: Christophe Coenraets</title>
      <link>https://forem.com/ccoenraets</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ccoenraets"/>
    <language>en</language>
    <item>
      <title>How to Build Progressive Web Apps with Offline Support using Lightning Web Components</title>
      <dc:creator>Christophe Coenraets</dc:creator>
      <pubDate>Thu, 30 Jul 2020 20:55:22 +0000</pubDate>
      <link>https://forem.com/salesforcedevs/how-to-build-progressive-web-apps-with-offline-support-using-lightning-web-components-2ce5</link>
      <guid>https://forem.com/salesforcedevs/how-to-build-progressive-web-apps-with-offline-support-using-lightning-web-components-2ce5</guid>
      <description>&lt;p&gt;In this blog post, I’ll guide you through an existing Progressive Web App (PWA) hosted online, and show you how it uses caching, works offline, and can be installed on your desktop. I’ll then walk you through the simple steps to create your own PWA with Lightning Web Components and the Workbox toolkit.&lt;/p&gt;

&lt;p&gt;Before we start, let’s quickly describe the terminology and libraries we use in this blog post.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Progressive Web Apps&lt;/strong&gt; are regular web applications that are augmented with “native app”-like capabilities. For example, progressive web apps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work offline&lt;/li&gt;
&lt;li&gt;Support web-push notification&lt;/li&gt;
&lt;li&gt;Can be installed on your desktop or the home screen of your mobile device&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To support these capabilities, PWAs use emerging browser standards and APIs, including the application manifest and service workers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;application manifest&lt;/strong&gt; is a JSON file that describes the application: name, start URL, icons, etc.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;service worker&lt;/strong&gt; is a script that the browser runs in the background, separately from the main browser thread, and can handle tasks like receiving web push notifications or intercepting network requests to support caching and offline mode. Caching and versioning assets is a nontrivial task. You can write your own logic in your service worker or you can use a library like &lt;a href="https://developers.google.com/web/tools/workbox" rel="noopener noreferrer"&gt;Workbox&lt;/a&gt; to simplify the process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lightning Web Components&lt;/strong&gt; is a lightweight open source UI framework based on the latest web standards. The framework makes it easy and fast to build web applications using web components. More information &lt;a href="https://lwc.dev/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Regular web app example
&lt;/h3&gt;

&lt;p&gt;Now let’s dive right in and take a look at a simple contact management application. Click &lt;a href="https://lwc-contacts.herokuapp.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to run the regular web app version. You should see a simple list of contacts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9hgusuu77c16dohcvl6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9hgusuu77c16dohcvl6g.png" alt="Simple list of contacts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try this:&lt;/p&gt;

&lt;p&gt;Note: We use Google Chrome to examine the application in the steps below. You should be able to achieve the same results using any browser supporting PWAs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the Developer Tools&lt;/li&gt;
&lt;li&gt;On the &lt;strong&gt;Network&lt;/strong&gt; tab, click the &lt;strong&gt;Online&lt;/strong&gt; dropdown in the toolbar and select &lt;strong&gt;Offline&lt;/strong&gt; to simulate offline mode (you can also turn off your wifi or unplug your network cable if you prefer)&lt;/li&gt;
&lt;li&gt;Reload the page: you should see the &lt;strong&gt;No Internet&lt;/strong&gt; page&lt;/li&gt;
&lt;li&gt;On the &lt;strong&gt;Network&lt;/strong&gt; tab, select &lt;strong&gt;Online&lt;/strong&gt; to go back online&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a standard web app: it only works while connected to the internet and doesn’t provide browser-level caching of assets.&lt;/p&gt;

&lt;h3&gt;
  
  
  PWA example
&lt;/h3&gt;

&lt;p&gt;Click &lt;a href="https://lwc-pwa-demo.herokuapp.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to run the progressive web app version of the same application. You should see the same list of contacts.&lt;/p&gt;

&lt;p&gt;Try this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If it’s the first time you access the PWA, refresh the page to allow the service worker (that has now been registered) to cache the configured runtime resources (like REST service calls)&lt;/li&gt;
&lt;li&gt;On the &lt;strong&gt;Network&lt;/strong&gt; tab, click the &lt;strong&gt;Online&lt;/strong&gt; dropdown in the toolbar and select &lt;strong&gt;Offline&lt;/strong&gt; to simulate offline mode&lt;/li&gt;
&lt;li&gt;Reload the page. The page is reloaded (from the cache) including static assets and runtime data&lt;/li&gt;
&lt;li&gt;On the &lt;strong&gt;Network&lt;/strong&gt; tab, select &lt;strong&gt;Online&lt;/strong&gt; to go back online&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s explore the application in the Google Developer Tools to understand how this works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the Google Developer Tools, select the &lt;strong&gt;Application&lt;/strong&gt; tab&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Service Workers&lt;/strong&gt; in the sidebar. Note that a service worker (sw.js) has been registered&lt;/li&gt;
&lt;li&gt;Expand &lt;strong&gt;Cache Storage&lt;/strong&gt; in the sidebar: You should see two entries: &lt;strong&gt;workbox-precache-…&lt;/strong&gt; and &lt;strong&gt;workbox-runtime-…&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;workbox-precache-..&lt;/strong&gt;: You should see all the static assets that have been precached&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;workbox-runtime-…&lt;/strong&gt;: You should see the cached runtime REST service invocation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbssxnd5lorlobffam8jt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbssxnd5lorlobffam8jt.png" alt="Google Developer Tools view"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: You can use the Offline checkbox at the top of the Application panel to simulate offline mode without having to go back to the Network panel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing a PWA on your home screen or desktop
&lt;/h3&gt;

&lt;p&gt;On mobile devices, you can install the app on your home screen where it will appear just like a native app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On iOS, access the app in Safari, tap the &lt;strong&gt;Share&lt;/strong&gt; button, scroll down, tap &lt;strong&gt;Add to Home Screen&lt;/strong&gt;, and tap &lt;strong&gt;Add&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;On Android, access the app in Chrome, tap Settings (upper right corner), slide down and tap &lt;strong&gt;Add to Home Screen&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Mac or Windows, you can install the app on your desktop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click Settings (three vertical dots in the upper right corner)&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Install LWC PWA Demo&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmp5wsx74044b4iape2hz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmp5wsx74044b4iape2hz.png" alt="Menu to install the LWC PWA Demo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The application is then available on your desktop like any other native app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5ww6hxtai3nwcc1so8vy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5ww6hxtai3nwcc1so8vy.png" alt="App on your desktop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we’ve seen the PWA in action, let’s see how to create it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a PWA with Lightning Web Components
&lt;/h3&gt;

&lt;p&gt;The easiest way to create a Lightning Web Components application is to use the &lt;a href="https://github.com/muenzpraeger/create-lwc-app" rel="noopener noreferrer"&gt;create-lwc-app&lt;/a&gt; CLI. Use the &lt;code&gt;-t pwa&lt;/code&gt; parameter to specify you want to create a PWA application. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`npx create-lwc-app my-contacts -t pwa`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will do two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a custom &lt;strong&gt;webpack-config.js&lt;/strong&gt; file in the &lt;strong&gt;scripts&lt;/strong&gt; folder and register the workbox plugin which will automatically generate a service worker (sw.js) during the build process&lt;/li&gt;
&lt;li&gt;Register the service worker in &lt;strong&gt;index.html&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With that default configuration, the static assets manipulated during the build process will automatically be precached when the application is loaded. You can fine-tune the default configuration. For example, let’s take a look at the registration of the workbox plugin in scripts/webpack-config.js for the contact-manager we just looked at. Note that we added a &lt;strong&gt;rutimeCaching&lt;/strong&gt; entry to cache calls to the &lt;strong&gt;api/contacts&lt;/strong&gt; REST service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new GenerateSW({
    swDest: 'sw.js',
    clientsClaim: true,
    skipWaiting: true,
    runtimeCaching: [
        {
            urlPattern: new RegExp('api/contacts$'),
            handler: 'StaleWhileRevalidate'
        }
    ]
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Source Code
&lt;/h3&gt;

&lt;p&gt;The source code for the application is available in &lt;a href="https://github.com/ccoenraets/lwc-pwa-demo" rel="noopener noreferrer"&gt;this&lt;/a&gt; GitHub repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;PWAs blur the line between web apps and native apps. They work offline, support push notifications and can be installed on your desktop or mobile device home screen. The Lightning Web Components CLI provides a fast and easy way to create PWAs that leverage the Workbox toolkit.&lt;/p&gt;

</description>
      <category>pwa</category>
      <category>lwc</category>
      <category>offline</category>
    </item>
  </channel>
</rss>
