ctucx.git: mqtt-webui

webui for mqtt, can be used to control/display data in mqtt-topics

commit ea83d448f936f8077b165820f4873f4e8544fa56
parent a0d45c2a73b464fcda3e3d57b5be8cbd2f792745
Author: Leah (ctucx) <git@ctu.cx>
Date: Thu, 8 Dec 2022 13:22:49 +0100

add service-worker
2 files changed, 97 insertions(+), 0 deletions(-)
M
src/webui.js
|
12
++++++++++++
A
www/sw.js
|
85
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/src/webui.js b/src/webui.js
@@ -191,6 +191,18 @@ const renderPage  = (pageData) => {
 const renderPages = (config) => html`${map(config.pages, (pageData) => renderPage(pageData))}`;
 
 
+const sw = navigator.serviceWorker;
+export let registration;
+if (sw) {
+	sw.register('sw.js', {
+		scope: './'
+	}).then(function(reg) {
+		console.log('serviceWorker registration succeeded.');
+		registration = reg;
+	});
+}
+
+
 window.addEventListener('hashchange', goToPage);
 window.addEventListener('DOMContentLoaded', async (event) => {
 	const instanceId = localStorage.getItem('instanceId') || shortid.generate();
diff --git a/www/sw.js b/www/sw.js
@@ -0,0 +1,85 @@
+'use strict';
+
+let preCache = [
+	'./',
+	'./manifest.json',
+	'./sw.js',
+	'./bundle.js',
+	'./bundle.css',
+	'./favicon-512x512.png',
+
+	'./icons/air_conditioning_indoor.png',
+	'./icons/air_conditioning_outdoor.png',
+	'./icons/alarm.png',
+	'./icons/bulb.png',
+	'./icons/cold.png',
+	'./icons/dome_camera.png',
+	'./icons/door_handle.png',
+	'./icons/doorbell.png',
+	'./icons/drop.png',
+	'./icons/electric_range.png',
+	'./icons/eye.png',
+	'./icons/garage.png',
+	'./icons/heater.png',
+	'./icons/houseplant.png',
+	'./icons/ip_camera.png',
+	'./icons/key.png',
+	'./icons/lock_a.png',
+	'./icons/lock_b.png',
+	'./icons/loudspeakers.png',
+	'./icons/pet.png',
+	'./icons/plug.png',
+	'./icons/pool.png',
+	'./icons/power.png',
+	'./icons/power_button.png',
+	'./icons/recycling.png',
+	'./icons/remote_control.png',
+	'./icons/router.png',
+	'./icons/smartphone.png',
+	'./icons/socket_f.png',
+	'./icons/socket_g.png',
+	'./icons/socket_i.png',
+	'./icons/solar_panel.png',
+	'./icons/sun.png',
+	'./icons/temperature.png',
+	'./icons/thermostat.png',
+	'./icons/tv.png',
+	'./icons/vacuum_cleaning_robot.png',
+	'./icons/ventilation.png',
+	'./icons/washer.png',
+	'./icons/weather.png'
+];
+
+const CACHE = 'cache-v1';
+
+self.addEventListener('install', function (evt) {
+	self.skipWaiting();
+	evt.waitUntil(caches.open(CACHE).then(function (cache) {
+		cache.addAll(preCache);
+	}));
+});
+
+self.addEventListener('fetch', function (evt) {
+	evt.respondWith(fromCache(evt.request).then(function (match) {
+		if (match) {
+			return match;
+		} else {
+			return fetch(evt.request);
+		}
+	}));
+});
+
+self.addEventListener('activate', function (event) {
+	event.waitUntil(clients.claim());
+	event.waitUntil(clients.claim().then(function () {
+		return caches.keys().then(function (cacheNames) {
+			return Promise.all(cacheNames.filter(c => c !== CACHE).map(c => caches.delete(c)));
+		});
+	}));
+});
+
+function fromCache (request) {
+	return caches.open(CACHE).then(function (cache) {
+		return cache.match(request);
+	});
+}