CodeCoupler Application Basics PWA Handling 🔒
What is this?
If the browser spots that a PWA has been loaded here, it gives the user a dialog box to install it locally. Once it's installed, you'll have some extra options for customising the window title bar of your local application.
This library lets you intercept the installation dialog and only open it when the user wants to.
Once the app's installed, the automatically set classes help you design the window title bar.
Important Notes:
- Handling the installation prompt works only in chromium based browsers!
- Read the chapter Prerequisites for further requirements for developing a PWA in a development environment!
- Instantiate the PWA class as early as possible in the page loading process. The class uses the
beforeinstallpromptevent, which is triggered only when the page is loading. It is not possible to intercept this event at a later stage.
Setup
Install Bundle:
Define Aliases, Externals and Assets:
Basic Usage
Or
The callback function will be executed whenever the browser wants to show the install button.
Handling Installation Prompt
Define a callback to get the prompt event handle. Whether this event is triggered depends on many factors. The browser will check that the PWA is configured correctly and is not running in an iframe, and that the PWA is already installed. Additionally, the PWA class will check whether the application is currently running in browser mode. This does not include full-screen mode.
Although there are several ways to define a handler, you should ensure that only one handler processes the installation prompt. This handler determines whether and when the user is shown the browser installation dialog.
There are two options:
Using the Promise
Use the result of the promise after initialisation to retrieve the prompt event handle. The promise will only ever be resolved, never rejected:
The promise resolves to the instance itself. The promise will only be called
once, and you will have to use the BeforeInstallPromptEvent object from the
instance property promptEvent. Bear in mind that the user can cancel the browser
dialogue and click your install button again. In this case, you will need to use
the BeforeInstallPromptEvent object from this property again, as it will have
changed.
Using the constructor
Define a callback during initialisation to handle the installation prompt:
The callback receives the BeforeInstallPromptEvent object as the first argument.
Bear in mind that the user can cancel the browser dialogue and click your
install button again. In this case, you must use the BeforeInstallPromptEvent
object again, as the arguments have changed. Also bear in mind that this
function will then be called multiple times. You must handle both of these
situations somehow.
CSS Classes & Handling Window Controls Overlay
If the application runs in windows control overlay mode, the body will receive the following
classes:
wco-visible: If the user hides the standard chrome title and the custom title should be shown.wco-invisible: The standard chrome title of the application is used.wco-controls-right: Window controls are on the right (like on Windows).wco-controls-left: Window controls are on the left (like on macOS).
If the windows control overlay mode is not supported the class wco-invisible will be added.
Example for a CodeCoupler UI root component using an wco header
wco-header.module.css (You have to define --color-primary somewhere to use this example):
root.js:
Reference
Constructor Options
The constructor expects an object with the following properties:
callbackInstallPrompt: Callback after installation prompt available. The callback receives theBeforeInstallPromptEventobject as the first argument.debug: If set totrue, the body initially receives the classeswco-visibleandwco-controls-rightinstead ofwco-invisible. If set toleft, the body initially receives the classeswco-visibleandwco-controls-left. This allows you to display the WCO header in normal browser mode. However, note that the height of the header does not correspond to the final result. This is only possible if the PWA is running inWindows control overlaymode, when the variableenv(titlebar-area-height)cannot be resolved.
Instance Member
pwaInstallPrompt(pwa)-
Resolves after installation prompt available.
Prerequisites
Service Worker
The application must register a service worker with a fetch event handler. You can use the Service Worker Loader library to do this:
Serve over HTTPS
In order to install the PWA, your application must be served over HTTPS. It is
recommended that you use the mkcert tool from
GitHub. This will install your own CA
which will be trusted by your system and your browsers and create a valid
certificate for your devServer.
Install mkcert on Linux (el9 platform)
First add the domain to your hosts file:
Now change to the root of your project directory and run:
If you are using Webpack you can run your devServer with these settings in your webpack.config.js:
Why not serve in another way over HTTPS?
In order to install the PWA, you must serve it over HTTPS. The simplest way to
do this is to set the value of devServer.server in your webpack.config.js to
https. Then, even if you have set another allowedHosts, you can open Chrome
and type https://localhost:XXXX. This will allow you to install the app.
However, whenever you start the app, you will be warned about an invalid certificate. This will prompt you to open the Chrome browser, accept the certificate, and then return to the app.
However, you may still experience issues with the service worker not loading. The service worker will only be loaded from a secure origin.
You can start the Chrome browser or the app with the following options:
--ignore-certificate-errors and
--unsafely-treat-insecure-origin-as-secure=https://localhost:XXXX.
The problem here is that the flag unsafely-treat-insecure-origin-as-secure
will not be enabled. You have to visit the page
chrome://flags#unsafely-treat-insecure-origin-as-secure and enable it
manually. Other resources say that you have to combine this with the option
--user-data-dir=/tmp/test-only-chrome-profile-dir, but this does not enable
the flag either.
However, if you want to experiment with these options you could set the property
open to this:
As you can see, there are many issues with using a self-signed certificate. It
is therefore recommended that you use the mkcert tool.
Web Manifest and Icons
Your application must provide a minimum web manifest, a favicon in different sizes, and two screenshots:
Minimum manifest.webmanifest file
Web Manifest and Icons using CodeCoupler Webpack
If you are using the CodeCoupler Webpack Configuration Factory you must provide
a file named favicon.png in the static directory and create a much smaller
manifest.webmanifest file. Both files must be in the static directory.
manifest.webmanifest file when using CodeCoupler Webpack
To enable testing in development mode, add this to the CodeCoupler Webpack Configuration:
Further Styling
We recommend defining the chrome colour of the standalone application in the HTML page as well as in the web manifest, even if it is already defined there. The web manifest will not be refreshed every time it is changed. Changes to the colour in the HTML page will be visible after every refresh.