From 01f4275f318b973da1066db99f1676d362fa3076 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 24 Jan 2024 16:55:14 +0100 Subject: Make RMU installable --- .editorconfig | 8 ++ MatrixRoomUtils.Web/wwwroot/index.html | 110 +++++++++++---------- MatrixRoomUtils.Web/wwwroot/mru.webmanifest | 16 --- MatrixRoomUtils.Web/wwwroot/rmu.webmanifest | 22 +++++ MatrixRoomUtils.Web/wwwroot/service-worker.js | 4 + .../wwwroot/service-worker.published.js | 55 +++++++++++ 6 files changed, 145 insertions(+), 70 deletions(-) delete mode 100644 MatrixRoomUtils.Web/wwwroot/mru.webmanifest create mode 100644 MatrixRoomUtils.Web/wwwroot/rmu.webmanifest create mode 100644 MatrixRoomUtils.Web/wwwroot/service-worker.js create mode 100644 MatrixRoomUtils.Web/wwwroot/service-worker.published.js diff --git a/.editorconfig b/.editorconfig index 3bc1adf..e98e832 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,5 @@ +root = true + [*] charset = utf-8 end_of_line = lf @@ -1795,3 +1797,9 @@ resharper_zero_index_from_end_highlighting = warning # ReSharper properties resharper_csharp_int_align_comments = true + +[*.html] +indent_size = 4 +tab_width = 4 +indent_style = space +ij_html_do_not_indent_children_of_tags = \ No newline at end of file diff --git a/MatrixRoomUtils.Web/wwwroot/index.html b/MatrixRoomUtils.Web/wwwroot/index.html index 7eef159..a40f38c 100644 --- a/MatrixRoomUtils.Web/wwwroot/index.html +++ b/MatrixRoomUtils.Web/wwwroot/index.html @@ -1,63 +1,65 @@ - - - - MatrixRoomUtils.Web - - - - - - - + + + + MatrixUtils.Web + + + + + + + + - -
- - - - -
-
+ +
+ + + + +
+
-
- An unhandled error has occurred. - Reload - 🗙 -
- - - + function getWindowDimensions() { + return { + width: window.innerWidth, + height: window.innerHeight + }; + } + + + + diff --git a/MatrixRoomUtils.Web/wwwroot/mru.webmanifest b/MatrixRoomUtils.Web/wwwroot/mru.webmanifest deleted file mode 100644 index ef1ee66..0000000 --- a/MatrixRoomUtils.Web/wwwroot/mru.webmanifest +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "Rory&::MatrixUtils", - "short_name": "MRU", - "description": "A collection of Matrix utilities.", - "icons": [ - { - "src": "icon-192.png", - "sizes": "192x192", - "type": "image/png" - } - ], - "start_url": "/index.html", - "display": "fullscreen", - "theme_color": "#052767", - "background_color": "#3A0647" -} diff --git a/MatrixRoomUtils.Web/wwwroot/rmu.webmanifest b/MatrixRoomUtils.Web/wwwroot/rmu.webmanifest new file mode 100644 index 0000000..27088c0 --- /dev/null +++ b/MatrixRoomUtils.Web/wwwroot/rmu.webmanifest @@ -0,0 +1,22 @@ +{ + "name": "Rory&::MatrixUtils", + "short_name": "RMU", + "description": "A collection of Matrix utilities.", + "icons": [ + { + "src": "icon-192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "icon-512.png", + "sizes": "512x512", + "type": "image/png" + } + ], + "start_url": "./", + "id": "./", + "display": "minimal-ui", + "theme_color": "#052767", + "background_color": "#3A0647" +} diff --git a/MatrixRoomUtils.Web/wwwroot/service-worker.js b/MatrixRoomUtils.Web/wwwroot/service-worker.js new file mode 100644 index 0000000..c6d0085 --- /dev/null +++ b/MatrixRoomUtils.Web/wwwroot/service-worker.js @@ -0,0 +1,4 @@ +// In development, always fetch from the network and do not enable offline support. +// This is because caching would make development more difficult (changes would not +// be reflected on the first load after each change). +self.addEventListener('fetch', () => { }); diff --git a/MatrixRoomUtils.Web/wwwroot/service-worker.published.js b/MatrixRoomUtils.Web/wwwroot/service-worker.published.js new file mode 100644 index 0000000..003e3e7 --- /dev/null +++ b/MatrixRoomUtils.Web/wwwroot/service-worker.published.js @@ -0,0 +1,55 @@ +// Caution! Be sure you understand the caveats before publishing an application with +// offline support. See https://aka.ms/blazor-offline-considerations + +self.importScripts('./service-worker-assets.js'); +self.addEventListener('install', event => event.waitUntil(onInstall(event))); +self.addEventListener('activate', event => event.waitUntil(onActivate(event))); +self.addEventListener('fetch', event => event.respondWith(onFetch(event))); + +const cacheNamePrefix = 'offline-cache-'; +const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`; +const offlineAssetsInclude = [ /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/ ]; +const offlineAssetsExclude = [ /^service-worker\.js$/ ]; + +// Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'. +const base = "/"; +const baseUrl = new URL(base, self.origin); +const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href); + +async function onInstall(event) { + console.info('Service worker: Install'); + + // Fetch and cache all matching items from the assets manifest + const assetsRequests = self.assetsManifest.assets + .filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url))) + .filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url))) + .map(asset => new Request(asset.url, { integrity: asset.hash, cache: 'no-cache' })); + await caches.open(cacheName).then(cache => cache.addAll(assetsRequests)); +} + +async function onActivate(event) { + console.info('Service worker: Activate'); + + // Delete unused caches + const cacheKeys = await caches.keys(); + await Promise.all(cacheKeys + .filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName) + .map(key => caches.delete(key))); +} + +async function onFetch(event) { + let cachedResponse = null; + if (event.request.method === 'GET') { + // For all navigation requests, try to serve index.html from cache, + // unless that request is for an offline resource. + // If you need some URLs to be server-rendered, edit the following check to exclude those URLs + const shouldServeIndexHtml = event.request.mode === 'navigate' + && !manifestUrlList.some(url => url === event.request.url); + + const request = shouldServeIndexHtml ? 'index.html' : event.request; + const cache = await caches.open(cacheName); + cachedResponse = await cache.match(request); + } + + return cachedResponse || fetch(event.request); +} -- cgit 1.4.1