Turning a Vaadin Flow Application Into an Installable PWA

In this chapter, you turn the completed CRM application into a progressive web application (PWA), so that users can install it.

What Is a PWA?

The term “PWA” is used to describe modern web applications that offer a native app-like user experience. PWA technologies make applications faster, more reliable, and more engaging. PWAs can be installed on most mobile devices and on desktop when using supported browsers. They can even be listed in the Microsoft Store and Google Play Store. You can learn more about the underlying technologies and features in the Vaadin PWA documentation.

Two main components enable PWA technologies:

  • ServiceWorker: a JavaScript worker file that controls network traffic and enables custom cache control.

  • Web app manifest: a JSON file that identifies the web application as an installable application.

Generating PWA Resources

Vaadin provides the @PWA annotation, which automatically generates the required PWA resources.

Add the @PWA annotation to MainLayout as follows:

MainLayout.java

  1. @Theme(themeFolder = "flowcrmtutorial")
  2. @PWA( 1
  3. name = "VaadinCRM", 2
  4. shortName = "CRM" 3
  5. )
  6. public class MainLayout extends AppLayout {
  7. ...
  8. }
  1. The @PWA annotation tells Vaadin to create a ServiceWorker and a manifest file.

  2. name is the full name of the application for the manifest file.

  3. shortName should be short enough to fit under an icon when installed, and should not exceed 12 characters.

Customize the Application Icon

You can override the default icon by replacing src/main/resources/META-INF/resources/icons/icon.png with your own 512px x 512px PNG icon.

You can use your own icon, or save the image below, by right-clicking and selecting Save Image.

example icon

Customize the Offline Page

Vaadin creates a generic offline fallback page that displays when the application is launched offline. Replacing this default page with a custom page that follows your own design guidelines makes your app more polished.

Use the code below to create offline.html in the META-INF/resources folder:

offline.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
  7. <title>Offline | Vaadin CRM</title>
  8. <style>
  9. body {
  10. display: flex;
  11. flex-direction: column;
  12. align-items: center;
  13. font-family: sans-serif;
  14. color: #555;
  15. }
  16. .content {
  17. width: 80%;
  18. }
  19. .offline-image {
  20. width: 100%;
  21. margin: 4em 0px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="content">
  27. <img src="./images/offline.png" alt="VaadinCRM is offline" class="offline-image"/>
  28. <h1>Oh deer, you're offline</h1>
  29. <p>Your internet connection is offline. Get back online to continue using Vaadin CRM.</p>
  30. </div>
  31. <script>
  32. window.addEventListener('online', () => window.location.reload()); 1
  33. </script>
  34. </body>
  35. </html>
  1. The JavaScript snippet reloads the page if the browser detects that it’s back online.

Add the following image (or use one of your own) to the META-INF/resources/images folder and name it offline.png.

example offline image

Make the files available offline by adding them to the @PWA annotation in MainLayout as follows:

MainLayout.java

  1. @PWA(
  2. name = "VaadinCRM",
  3. shortName = "CRM",
  4. offlinePath="offline.html",
  5. offlineResources = { "./images/offline.png"} 1
  6. )
  1. offlineResources is a list of files that Vaadin makes available offline through the ServiceWorker.

Restart the app. You can now install the application on supported browsers.

Testing the Offline Page

Shut down the server in IntelliJ and refresh the browser (or launch the installed app). You should now see the custom offline page.

custom offline page

In the next chapter, you will add tests to the application: both unit tests and in-browser tests.

migration assistance

Download free e-book.
The complete guide is also available in an easy-to-follow PDF format.

https://pages.vaadin.com/en/build-a-modern-web-app-with-spring-boot-vaadin-pdf

Open in anew tab

Show code

render-banner.ts

  1. export class RenderBanner extends HTMLElement {
  2. connectedCallback() {
  3. this.renderBanner();
  4. }
  5. renderBanner() {
  6. let bannerWrapper = document.getElementById('tocBanner');
  7. if (bannerWrapper) {
  8. return;
  9. }
  10. let tocEl = document.getElementById('toc');
  11. // Add an empty ToC div in case page doesn't have one.
  12. if (!tocEl) {
  13. const pageTitle = document.querySelector(
  14. 'main > article > header[class^=PageHeader-module--pageHeader]'
  15. );
  16. tocEl = document.createElement('div');
  17. tocEl.classList.add('toc');
  18. pageTitle?.insertAdjacentElement('afterend', tocEl);
  19. }
  20. // Prepare banner container
  21. bannerWrapper = document.createElement('div');
  22. bannerWrapper.id = 'tocBanner';
  23. tocEl?.appendChild(bannerWrapper);
  24. // Banner elements
  25. const text = document.querySelector('.toc-banner-source-text')?.innerHTML;
  26. const link = document.querySelector('.toc-banner-source-link')?.textContent;
  27. const bannerHtml = `<div class='toc-banner'>
  28. <a href='${link}'>
  29. <div class="toc-banner--img"></div>
  30. <div class='toc-banner--content'>${text}</div>
  31. </a>
  32. </div>`;
  33. bannerWrapper.innerHTML = bannerHtml;
  34. // Add banner image
  35. const imgSource = document.querySelector('.toc-banner-source .image');
  36. const imgTarget = bannerWrapper.querySelector('.toc-banner--img');
  37. if (imgSource && imgTarget) {
  38. imgTarget.appendChild(imgSource);
  39. }
  40. }
  41. }