...
Configure the Enabled option as Yes
...
Settings:
Name | Default value | Note |
---|---|---|
Enabled | No | |
Name | required Application name displayed on the splash screen below the icon. | |
Short name | required Application name displayed in the application drawer or on the home screen. | |
Description | required Application description, not displayed anywhere but can be used by app stores in the future. | |
Start URL when not logged in | Home Page | Page which should be loaded when users open the application and is not logged in. |
Start URL when logged in | Home Page | Page which should be loaded when users open the application and is logged in. It can also Custom Url |
Custom Start URL when logged in | f.e. sales/order/history | |
Offline page URL | About us | Page which should be shown when user device is offline. |
Display mode | Minimal UI | required The way the installed application will be displayed. Please refer to https://web.dev/add-manifest/#display for a detailed description of each mode. |
Background color | #FFFFFF | required Background of the splash screen, the one displayed when the application is loading for the first time. |
Theme color | #FFFFFF | required General theme color of the application, used as the status bar background. |
Categories | shopping,fitness,sports | required List of categorizations you want to apply to your site. Categories should be lower cased and comma separated. There are no pre-defined categories, but the W3C maintains a list of known categories. |
Debug mode enabled | No | Enables service worker debug mode. |
Display modes
By default, minimal-ui mode is selected.
...
fullscreen
Opens the web application with browser UI elements hidden and takes up the entirety of the available display area.standalone
Opens the web application to look and feel like a standalone native application. This can include the application having a different window, its own icon in the application launcher, etc. In this mode, the user agent will exclude standard browser UI elements such as an URL bar, but can include other system UI elements such as a status bar and/or system back button.minimal-ui
This mode is similar to standalone, but provides the end-user with some means to access a minimal set of UI elements for controlling navigation (i.e., back, forward, reload, and perhaps some way of viewing the document's address). A user agent can include other platform specific UI elements, such as "share" and "print" buttons or whatever is customary on the platform and user agent.browser
Opens the web application using the platform-specific convention for opening hyperlinks in the user agent (e.g., in a browser tab or a new window).
Frontend
Service
...
workers
Basics
Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance. https://developer.chrome.com/docs/workbox/service-worker-overview/
. With proper service worker and manifest implementations, browsers can proactively prompt users to add your app to their homescreen, which can lead to higher users engagement.
Service worker lifecycle
Before the body end register service worker template is added:
Code Block |
---|
<block name="pwa.service-worker.register" template="MageSuite_Pwa::service-worker/register.phtml" after="-" ifconfig="pwa/general/is_enabled"/> |
pwa_serviceworker_index.xml
layout composes service worker script ( service-worker.js ) with smaller templates:
service-worker.phtml
...
service-worker/workbox.phtml
...
service-worker/static.phtml
...
service-worker/media.phtml
...
service-worker/mobile-navigation.phtml
...
service-worker/product-reviews.phtml
...
service-worker/google-fonts.phtml
...
On the very first visit to a web page that installs a new service worker, the initial visit to a page provides its baseline functionality while the service worker downloads. After a service worker is installed and activated, it controls the page to offer improved reliability and speed. https://developer.chrome.com/docs/workbox/service-worker-overview/
Caching strategies
An indispensable aspect of service worker technology is the Cache
interface, which is a caching mechanism wholly separate from the HTTP cache. The Cache
interface is programmable through Javascript. Caching responses for network requests can be based on whatever logic is best for a given website. These scenarios are called caching strategies
https://developer.chrome.com/docs/workbox/service-worker-overview/
An asynchronous and event-driven API
Service workers accommodate this asynchronicity through an event-driven API. Events can be registered using a familiar addEventListener
API. All of these events can potentially interact with the Cache
interface. async
and await
, those JavaScript features can also be used to simplify service worker.
Precaching and runtime caching
Info |
---|
Precaching is the process of caching assets ahead of time, typically during a service worker's installation. With precaching, key static assets and materials needed for offline access or subsequent pages can be downloaded and stored in a |
Info |
---|
Runtime caching is when a caching strategy is applied to assets as they are requested from the network during runtime. It guarantees offline access to pages and assets the user has already visited. |
Isolation form main thread
Service workers occur on their own threads. This means service workers' tasks won't compete for attention with other tasks on the main thread.
Debugging service worker
...
Workbox
Why do we use workbox?
There's lots of complex interactions that are hard to get right [in service workers APi and logic]. Workbox is a set of modules that simplify common service worker routing and caching. Each module available addresses a specific aspect of service worker development. Workbox aims to make using service workers as easy as possible, while allowing the flexibility to accommodate complex application requirements where needed. https://developer.chrome.com/docs/workbox/what-is-workbox/
How service worker is composed in MageSuite
Registration
Before the body end register service worker template is added:
Code Block |
---|
<block name="pwa.service-worker.register" template="MageSuite_Pwa::service-worker/register.phtml" after="-" ifconfig="pwa/general/is_enabled"/> |
Code Block |
---|
<?php
/** @var $block \MageSuite\Pwa\Block\Data */
$url = $block->getBaseUrl() . 'service-worker.js';
?>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('<?= /* @noEscape */ $url ?>');
}
</script>
|
service-worker.js file
pwa_serviceworker_index.xml
layout composes service worker script ( service-worker.js ) with smaller templates.
templates/service-worker.phtml
- parent template
Service worker file chunks
Import workbox script
service-worker/workbox.phtml - import workbox script and set optional debug mode (can be configured in the admin panel)
Detect requests for static files:
service-worker/static.phtml
Cache name: static
Route:
Code Block |
---|
new RegExp('<?= /* @noEscape */ $staticBaseUrl ?>.+\.(?:png|gif|jpg|jpeg|webp|svg|js|css|html|json|woff|woff2)$'), |
Code Block |
---|
new workbox.strategies.CacheFirst({
cacheName: 'static',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 300,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
}) |
Use CacheFirst
strategy: https://developer.chrome.com/docs/workbox/modules/workbox-strategies/#cache-first-cache-falling-back-to-network combined with workbox expiration plugin: https://developer.chrome.com/docs/workbox/modules/workbox-expiration/ with expiration time set to 30 days
Result: If there is an in the cache, it will served from the cache, otherwise there will be a network request and the response will be cached. An asset cache expires after 30 days. limit the number of entries in a cache to 300;
Detect requests for media files:
service-worker/media.phtml
Cache name: media
Use CacheFirst
strategy: combined with workbox expiration plugin: with expiration time set to 30 days and number of entries: 300
Mobile navigation
service-worker/mobile-navigation.phtml
Cache name: mobile-navigation
Use CacheFirst
strategy and limit the number of entries in a cache to 1.
Product reviews
service-worker/product-reviews.phtml
Cache name: product-reviews
Use StaleWhileRevalidate
strategy.
Result: Respond to the request with a cached response if available, falling back to the network request if it's not cached. The network request is then used to update the cache. https://developer.chrome.com/docs/workbox/modules/workbox-strategies/#stale-while-revalidate
Info |
---|
This is a fairly common strategy where having the most up-to-date resource is not vital to the application. |
Offline pages
service-worker/offline-pages.phtml
when a page is reloaded navigation request is added to offline-pages
cache for 1 hour.
Cache name: offline-pages
Offline fallback
service-worker/offline-fallback.phtml
Result: Display offline page when a user if offline;
Example of composer service-worker script: https://demo.magesuite.io/service-worker.js
Debugging workbox
...
Manifest
pwa_manifest_index.xml
layout adds a link to manifest.json, which is generated based on admin configuration
...