All Projects → Chalarangelo → Pwapp Demo

Chalarangelo / Pwapp Demo

Licence: mit
A very simple progressive web app demo that will help you get started.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Pwapp Demo

Prelude
A web app for practicing musical sight reading skills
Stars: ✭ 24 (+84.62%)
Mutual labels:  service-worker, progressive-web-app
Android Pwa Wrapper
Android Wrapper to create native Android Apps from offline-capable Progressive Web Apps
Stars: ✭ 265 (+1938.46%)
Mutual labels:  progressive-web-app, service-worker
jekyll-pwa-workbox
A Jekyll plugin using Workbox to make your PWA / Website available offline.
Stars: ✭ 22 (+69.23%)
Mutual labels:  service-worker, progressive-web-app
Vue Wordpress Pwa
An offline-first SPA using Vue.js, the WordPress REST API and Progressive Web Apps
Stars: ✭ 665 (+5015.38%)
Mutual labels:  progressive-web-app, service-worker
Pwa Book Cn
第一本 PWA 中文书
Stars: ✭ 3,498 (+26807.69%)
Mutual labels:  progressive-web-app, service-worker
gatsby-pwa-demo
PWA Example: Progressive Web App E-Commerce with Gatsby.js
Stars: ✭ 68 (+423.08%)
Mutual labels:  service-worker, progressive-web-app
Your First Pwapp
Code associated with Your First Progressive Web App codelab
Stars: ✭ 619 (+4661.54%)
Mutual labels:  progressive-web-app, service-worker
next-with-workbox
Higher order Next.js config to generate service workers
Stars: ✭ 22 (+69.23%)
Mutual labels:  service-worker, progressive-web-app
Sw Precache
Service Worker Precache is a module for generating a service worker that precaches resources. It integrates with your build process. Once configured, it detects all your static resources (HTML, JavaScript, CSS, images, etc.) and generates a hash of each file's contents. Information about each file's URL and versioned hash are stored in the generated service worker file, along with logic to serve those files cache-first, and automatically keep those files up to date when changes are detected in subsequent builds.
Stars: ✭ 5,276 (+40484.62%)
Mutual labels:  progressive-web-app, service-worker
Super Progressive Web Apps
SuperPWA helps to convert your WordPress website into Progressive Web Apps instantly. PWA (Progressive Web Apps) demo at : https://superpwa.com and Plugin :
Stars: ✭ 304 (+2238.46%)
Mutual labels:  progressive-web-app, service-worker
Upup
✈️ Easily create sites that work offline as well as online
Stars: ✭ 4,777 (+36646.15%)
Mutual labels:  progressive-web-app, service-worker
Expense Manager
💸 Take control back. Track your everyday spendings.
Stars: ✭ 409 (+3046.15%)
Mutual labels:  progressive-web-app, service-worker
exploration-service-worker
Let's get started with ServiceWorker
Stars: ✭ 14 (+7.69%)
Mutual labels:  service-worker, progressive-web-app
react-app-rewire-workbox
Customise the service worker for create-react-app apps without ejecting - using Google's Workbox webpack plugins instead of the old sw-precache
Stars: ✭ 44 (+238.46%)
Mutual labels:  service-worker, progressive-web-app
java-pwa
Progressive Web Apps in Java
Stars: ✭ 48 (+269.23%)
Mutual labels:  service-worker, progressive-web-app
Pwa Fundamentals
👨‍🏫 Mike & Steve's Progressive Web Fundamentals Course
Stars: ✭ 256 (+1869.23%)
Mutual labels:  progressive-web-app, service-worker
affilicats
🐈 Progressive Web App demo that showcases flaky network resilience measures (📶 or 🚫📶).
Stars: ✭ 65 (+400%)
Mutual labels:  service-worker, progressive-web-app
chicio.github.io
👻 Fabrizio Duroni (me 😄) personal website. Created using GatsbyJS, Styled Components, Storybook, Typescript, tsParticles, GitHub pages, Github Actions, Upptime.
Stars: ✭ 20 (+53.85%)
Mutual labels:  service-worker, progressive-web-app
Progressive Web Apps Book
All of the code for "Progressive Apps" - a book by Dean Hume
Stars: ✭ 270 (+1976.92%)
Mutual labels:  progressive-web-app, service-worker
Sw Toolbox
A collection of tools for service workers
Stars: ✭ 3,649 (+27969.23%)
Mutual labels:  progressive-web-app, service-worker

Progressive Web App Demo

A very simple progressive web app demo that will help you get started. Below you can find a quick rundown of what the demo does. The resulting progressive web app can be found here.

index.html

A very straightforward HTML page, stylized using my very own mini.css framework. It contains some simple controls for the user to interact with and some dummy elements to print results and status messages.

One point of interest in this file is the manifest declaration, which looks something like this:

<link rel="manifest" href="./manifest.json">

js/app.js

A simple piece of Javascript code to test the functionality of the progressive web app. We send requests to the JSONPlaceholder API, based on the user's input and retrieve some sample data.

Note that inside the document's loading event, we added the following code:

if ('serviceWorker' in navigator) {
	navigator.serviceWorker
		.register('./service-worker.js')
		.then(function() { console.log('Registered service worker!'); });
}

This code registers the progressive web app's service worker.

manifest.json

The manifest of the progressive web app looks something like this:

{
 	"name": "Progressive Web App Demo",
	"short_name": "PWA Demo",
	"icons": [{
		"src": "./icons/pwa-256x256.png",
		"sizes": "256x256",
		"type": "image/png"
		}],
	"start_url": "./index.html",
	"display": "standalone",
	"background_color": "#c62828",
	"theme_color": "#b71c1c"
}

In this file we define the human-readable name and short_name descriptors for our progressive web app, the icons array for the set of images that will serve as the progressive web app's icon set (we only have one in this demo), the start_url for our application, display mode and the color scheme via background_color and theme_color.

You can find more information on the structure of the progressive web app's manifest here.

service-worker.js

Last, but not least, the service worker is what makes a progressive web app what it is. We first define a name for the cache to use and what resources to be cached, like this:

var cacheName = 'demoPWA-v1';
var filesToCache = [
	'./',
	'./index.html',
	'./js/app.js',
	'./icons/pwa-256x256.png'
];

Then, we deal with the various events. install comes first and it's the event that's fired when you first visit the page. What we want to do in our install event is cache the progressive web app's shell. We achieve this using the following code:

self.addEventListener('install', function(e) {
	console.log('[demoPWA - ServiceWorker] Install event fired.');
	e.waitUntil(
		caches.open(cacheName).then(function(cache) {
			console.log('[demoPWA - ServiceWorker] Caching app shell...');
			return cache.addAll(filesToCache);
		})
	);
});

Now, for the activate event, we want to deal with updating the cache, as necessary. To do this, we used the code below:

self.addEventListener('activate', function(e) {
	console.log('[demoPWA - ServiceWorker] Activate event fired.');
	e.waitUntil(
		caches.keys().then(function(keyList) {
			return Promise.all(keyList.map(function(key) {
				if (key !== cacheName) {
					console.log('[demoPWA - ServiceWorker] Removing old cache...', key);
					return caches.delete(key);
				}
			}));
		})
	);
	return self.clients.claim();
});

Finally, we deal with the fetch event, which is fired whenever we send a request from our page. What we want to do is check if we have the resource cached and try to serve it from the cache, otherwise we will fetch the resource from the URL, as normal. We also added some error handling at the end to deal with certain requests that returned errors when offline. After all of that, we eneded up with the following code:

self.addEventListener('fetch', function(e) {
	console.log('[demoPWA - ServiceWorker] Fetch event fired.', e.request.url);
	e.respondWith(
		caches.match(e.request).then(function(response) {
			if (response) {
				console.log('[demoPWA - ServiceWorker] Retrieving from cache...');
				return response;
			}
			console.log('[demoPWA - ServiceWorker] Retrieving from URL...');
			return fetch(e.request).catch(function(e){
				console.log('[demoPWA - ServiceWorker] Fetch request failed!');
			});
		})
	);
});

Resources and tutorials

License

This project is licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].