findPWA - Directory of Progressive Web Apps

breadcrumb-icon

Develop a Progressive Web App

Attributes of Progressive Web Apps

  • Starts fast, stays fast
  • Works in any browser
  • Responsive to any screen size
  • Provides a custom offline page
  • Is installable

PWA Checklist by web.dev

What you will need

  • https
  • Manifest file
  • Service worker
  • Offline page
  • responsive website

Google Lighthouse

While developing it is really important to always check your website with the Lighthouse Audit.

With this tool you get information about performance, accessibility, best practices, search engine optimization and progressive web apps.

Content

Basic Code

The manifest file

When you install a PWA, the browser gets information about the app from the manifest file.

The file is written in json and needs to be called in the index file.


index.html
	<link rel="manifest" type="application/manifest+json; charset=utf-8" href="/manifest.json">
manifest.json (basic)
	{
		"dir": "ltr",
		"lang": "en",
		"name": "findPWA",
		"short_name": "findPWA",
		"description": "Find the best Progressive Web Apps.",
		"icons": [
			{
				"src": "/images/icons/findpwa_logo-192.png",
				"sizes": "192x192",
				"type": "image/png",
				"purpose": "maskable"
			}, {
				"src": "/images/icons/findpwa_logo-512.png",
				"sizes": "512x512",
				"type": "image/png",
				"purpose": "maskable any"
			}
		],
		"display": "standalone",
		"orientation": "portrait",
		"start_url": "/",
		"theme_color": "#cb3333",
		"background_color": "#fff"
	}

Manifest Schema by W3C

The service worker

The service worker is the brain of the PWA which controls the cache and other services like push notification.

The file is written in javascript and must be registered in the main script file. The offline page is a simple html file in which is being said that the user must go online.


script.js
	//Register service worker.
	if ('serviceWorker' in navigator) {
	window.addEventListener('load', () => {
		navigator.serviceWorker.register('/service-worker.js')
			.then((reg) => {
			//console.log('Service worker registered.', reg);
			});
	});
	}
service-worker.js by Google
	/*
	Copyright 2015, 2019 Google Inc. All Rights Reserved.
	 Licensed under the Apache License, Version 2.0 (the 'License');
	 you may not use this file except in compliance with the License.
	 You may obtain a copy of the License at
	 http://www.apache.org/licenses/LICENSE-2.0
	 Unless required by applicable law or agreed to in writing, software
	 distributed under the License is distributed on an 'AS IS' BASIS,
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 See the License for the specific language governing permissions and
	 limitations under the License.
	*/
	
	// Incrementing OFFLINE_VERSION will kick off the install event and force
	// previously cached resources to be updated from the network.
	const OFFLINE_VERSION = 1;
	const CACHE_NAME = 'offline';
	// Customize this with a different URL if needed.
	const OFFLINE_URL = 'offline.php';
	
	self.addEventListener('install', (event) => {
	  event.waitUntil((async () => {
		const cache = await caches.open(CACHE_NAME);
		// Setting {cache: 'reload'} in the new request will ensure that the response
		// isn't fulfilled from the HTTP cache; i.e., it will be from the network.
		await cache.add(new Request(OFFLINE_URL, {cache: 'reload'}));
	  })());
	});
	
	self.addEventListener('activate', (event) => {
	  event.waitUntil((async () => {
		// Enable navigation preload if it's supported.
		// See https://developers.google.com/web/updates/2017/02/navigation-preload
		if ('navigationPreload' in self.registration) {
		  await self.registration.navigationPreload.enable();
		}
	  })());
	
	  // Tell the active service worker to take control of the page immediately.
	  self.clients.claim();
	});
	
	self.addEventListener('fetch', (event) => {
	  // We only want to call event.respondWith() if this is a navigation request
	  // for an HTML page.
	  if (event.request.mode === 'navigate') {
		event.respondWith((async () => {
		  try {
			// First, try to use the navigation preload response if it's supported.
			const preloadResponse = await event.preloadResponse;
			if (preloadResponse) {
			  return preloadResponse;
			}
	
			const networkResponse = await fetch(event.request);
			return networkResponse;
		  } catch (error) {
			// catch is only triggered if an exception is thrown, which is likely
			// due to a network error.
			// If fetch() returns a valid HTTP response with a response code in
			// the 4xx or 5xx range, the catch() will NOT be called.
			console.log('Fetch failed; returning offline page instead.', error);
	
			const cache = await caches.open(CACHE_NAME);
			const cachedResponse = await cache.match(OFFLINE_URL);
			return cachedResponse;
		  }
		})());
	  }
	
	  // If our if() condition is false, then this fetch handler won't intercept the
	  // request. If there are any other fetch handlers registered, they will get a
	  // chance to call event.respondWith(). If no fetch handlers call
	  // event.respondWith(), the request will be handled by the browser as if there
	  // were no service worker involvement.
	});

Service Worker Sample: Custom Offline Page Sample by Google Chrome

Responsive Website

To get your website responsive for all kinds of devices you will need to add this html snipped:

index.html
	<meta name="viewport" content="width=device-width, initial-scale=1.0">

Icons

Maskable Icons

Your icons should be simple and maskable. Check it out with Maskable.app.

Maskable icons by web.dev