Skip to content
Back to log

PWA basics: Installation, manifest and offline operation

Chainabit's Chao · · 9 min read

In short

Basic information about PWA installation, web app manifest fields, offline operation and APIs used for background operations.

A Progressive Web App (PWA) is a web application built with web platform technologies that can provide an experience similar to a platform-specific application. According to MDN, a PWA can run on different platforms and devices from a single codebase. It can be installed on a device, work offline and in the background, and interact with the device and other installed applications.

Installation, the web app manifest and the service worker concern different aspects of a PWA. MDN describes installation as making a PWA appear on a device as a persistent application. The W3C Web Application Manifest standard defines the parameters to use when the application launches. The service worker APIs described by MDN can be used for offline operation and background operations.

What does installation mean?

MDN defines installability as one of the distinguishing features of a PWA. An installed PWA appears on the device as a persistent application and can be launched directly from the operating system like other applications. According to web.dev, installed PWAs can be launched from the home screen, dock, taskbar or shelf. Users can search for them on the device and switch between them using the app switcher.

According to the W3C Web Application Manifest standard, a user agent may provide a way for a user to install a web application on their device. When an installed web application is launched, the manifest fields or their defaults are applied. Manifest properties are not applied in this way to a web page opened from a traditional bookmark.

W3C gives examples of how installation can be presented. The application may be shown with a named icon on the home screen or in the launcher. A user agent may also install the application in its own list of bookmarks. MDN also notes that developers can provide an interface within the application to invite users to install it.

What does a web app manifest contain?

According to W3C, an application manifest is a JSON file containing startup parameters and application defaults to be used when a web application launches. These include the application's name, links to its icons and the preferred URL at launch. The manifest can also define the screen orientation, display mode and URL scope of the application.

The fields defined by the standard include name, short_name, icons, id, start_url, scope, display, orientation, theme_color, background_color and shortcuts. W3C states that these fields are optional in the manifest and can appear in any order. The lang field specifies the language for the values of localizable fields; dir defines the default text direction for those fields.

W3C also shows an example in which the manifest is linked using an HTML link element. In the example, the rel value is manifest, and the href value is the name of the manifest file. The same example also contains separate HTML elements for the application name and icons.

Name and icons

name and short_name specify the application's name. According to W3C, the short name can be used where space is limited, such as beneath an icon. When both fields are defined, implementers decide which name to use according to the situation. If one of the fields is missing or is not of the appropriate type, the user agent may use the other.

icons lists icons that can be used to represent the application in different places. W3C's example contains icons of the same size in different formats. The user agent selects an icon from those provided that is suitable for where it will be displayed. An icon's type field can specify its format, and its sizes field can specify its dimensions.

W3C also defines the purpose field for icons. monochrome specifies an icon that can be used where a single-color fill is needed. maskable indicates that the icon was prepared with a mask in mind. any is the default value and can be used where no special purpose is required.

For a maskable icon, the user agent may mask the parts outside the safe area. According to W3C, pixels inside the safe area cannot be made transparent. For a monochrome icon, alpha information is used instead of color information; the user agent may display the icon with a fill color of its choosing.

Launch URL and scope

start_url concerns the URL preferred for opening when the application is launched. According to W3C, start_url is used when an application is launched without a target URL being provided. id represents the identity of the application. The user agent can use this identity to determine whether a manifest describes a separate application or a manifest that replaces one for an installed application.

scope determines which URLs the manifest applies to. In W3C's example, when the scope value is /, the manifest applies to all pages within the origin. When the value is /racer/, pages under that path are in scope; a different path is outside the scope. The standard supports a single scope path.

The browser continues to apply the manifest as the user navigates URLs within scope. When the user navigates outside the scope, the manifest no longer applies and the browser's own defaults take effect. W3C gives the example of displaying the application as a normal web page in a browser tab instead of full screen in this case. How entry into and exit from scope are handled is left to the implementer.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

When scope is not specified, the default scope is derived from the path of the start_url field. In W3C's example, if the launch URL is /pages/welcome.html, the default scope is /pages/ on the same origin. If start_url is also absent, the URL of the page from which installation took place is used for the scope.

Display and colors

display specifies the application's preferred display mode. The modes defined by W3C are fullscreen, standalone, minimal-ui and browser. If the user agent does not support the requested mode, it uses a mode in the fallback chain defined by the standard. For example, the chain for standalone goes first to minimal-ui, then to browser.

If display is missing or invalid, the applied mode is browser. The user agent may change the applied mode for security reasons. W3C gives the example of the application navigating to an address outside its scope. The user may also be given the option to switch to another mode.

According to W3C, the applied display mode is indicated by the display-mode media feature, which can be accessed through CSS or JavaScript. This value may differ from the mode specified in the manifest. The media feature may also reflect the change when a user puts a page to which no manifest applies into full-screen mode.

theme_color specifies the default theme color of the application context. background_color defines the expected background color of the application. According to W3C, the user agent may use this color to draw the background while the application files are not yet available. When the application's stylesheet is available, background_color is not used in its place.

Shortcuts

shortcuts contains shortcut items that provide access to key tasks in the web application. According to W3C, each item represents a task or page link. An item may have name, short_name, description, url and icons fields. The user agent may use this information to create a menu shown when the application icon is interacted with in the operating system.

A shortcut's url value is the address opened within the scope of the processed manifest. When the shortcut is activated, the application is launched with this address. The user agent or operating system decides how shortcuts are presented and how many are shown to the user.

Offline operation and background operations

According to MDN, PWAs can use various technologies to provide functionality when the device's network connection is intermittent and to perform operations in the background when the main application is not running. Service workers and related web APIs play a role in these tasks.

Cache is a mechanism used to store HTTP responses persistently. Stored content can be reused when the application is offline. FetchEvent is triggered within the service worker for HTTP requests made by the client PWA. The service worker can send the request to the server and save the response for later use, or respond to the request with a response previously stored in the cache.

MDN describes several separate APIs for background operations. The Background Synchronization API can defer tasks to run within the service worker when a stable network connection becomes available. The Web Periodic Background Synchronization API allows tasks to be registered to run at intervals with a network connection. The Background Fetch API is intended for service workers to manage potentially lengthy downloads, such as video or audio files.

The service worker can also communicate with the associated client PWA. According to MDN, Client.postMessage() allows the service worker to send a message to the client PWA. The Broadcast Channel API can be used to establish a two-way communication channel between the service worker and the client PWA. MDN also defines IndexedDB as a client-side storage API for structured data, including files.

Interaction with other applications and the device

MDN defines the Web Share API as a mechanism for sharing content such as text, links and files with other applications selected by the user on their device. The Notifications API can send notifications displayed at the operating system level. The Badging API can set a badge on the application icon.

According to web.dev, installed PWAs can register to accept content from other applications or become the default application for opening certain file types. MDN also describes the ability to present an application's frequently used actions in the operating system's application shortcut menu.

The Window Controls Overlay API described by MDN allows the default window title bar to be hidden in PWAs installed on desktop operating systems. This allows the application to be displayed across the entire surface of the window.

Limitations

web.dev states that some capabilities remain beyond the reach of the web platform. According to W3C, manifest fields are optional; implementers may ignore fields they do not recognize or support. For the display mode, a supported fallback may also be applied instead of the requested value.

The use of Cache and FetchEvent described by MDN makes it possible to serve a previously stored response again when offline. The manifest as defined by W3C carries the application's launch parameters and defaults.

Ready to start?

Download the app and continue from your phone.

End of Entry