From c9f3a181f3c294cd607755225ed4063fee7a928a Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Mon, 3 Feb 2020 22:37:40 +0200 Subject: [PATCH 1/5] Move app caching to a webworker --- app.js | 16 +++-- views/adminindex.html | 10 +-- views/clockviews/shared.html | 2 +- views/foodindex.html | 10 +-- views/index.html | 30 ++++++--- views/nightscout.appcache | 37 ---------- views/profileindex.html | 10 +-- views/reportindex.html | 16 ++--- views/service-worker.js | 127 +++++++++++++++++++++++++++++++++++ 9 files changed, 182 insertions(+), 76 deletions(-) delete mode 100644 views/nightscout.appcache create mode 100644 views/service-worker.js diff --git a/app.js b/app.js index b718e3772e9..16ee906007c 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,7 @@ const bodyParser = require('body-parser'); const path = require('path'); const fs = require('fs'); +const ejs = require('ejs'); function create (env, ctx) { var app = express(); @@ -92,6 +93,15 @@ function create (env, ctx) { } app.locals.cachebuster = cacheBuster; + app.get("/sw.js", (req, res) => { + res.setHeader('Content-Type', 'application/javascript'); + res.send(ejs.render(fs.readFileSync( + require.resolve(`${__dirname}/views/service-worker.js`), + { encoding: 'utf-8' }), + { locals: app.locals} + )); + }); + if (ctx.bootErrors && ctx.bootErrors.length > 0) { app.get('*', require('./lib/server/booterror')(ctx)); return app; @@ -178,12 +188,6 @@ function create (env, ctx) { app.use("/clock", clockviews); - app.get("/appcache/*", (req, res) => { - res.render("nightscout.appcache", { - locals: app.locals - }); - }); - app.use('/api', bodyParser({ limit: 1048576 * 50 }), apiRoot); diff --git a/views/adminindex.html b/views/adminindex.html index c9bb631cfeb..9b4ff750d2e 100644 --- a/views/adminindex.html +++ b/views/adminindex.html @@ -26,10 +26,10 @@ - + - + <% include preloadCSS %> @@ -39,8 +39,8 @@ <%- include('partials/authentication-status') %> - - - + + + diff --git a/views/clockviews/shared.html b/views/clockviews/shared.html index cb27b137dfb..beac7dc0f2e 100644 --- a/views/clockviews/shared.html +++ b/views/clockviews/shared.html @@ -40,7 +40,7 @@
- + - - + + + diff --git a/views/index.html b/views/index.html index 153e187b7f7..21915a17f9c 100644 --- a/views/index.html +++ b/views/index.html @@ -1,7 +1,5 @@ - - manifest="appcache/nightscout-<%= locals.cachebuster %>.appcache" - <% } %>> + @@ -29,9 +27,9 @@ - - - + + + -<% include preloadCSS %> - - -
-
-

-

Loading the client

-
-
-
-
-
-
-
-
- <%- include('partials/toolbar') %> - -
- -
-
-
- - -
-
-
- -
-
-
---
-
-
-
-
-
- -
    -
  • Hours:
  • -
  • 2
  • -
  • 3
  • -
  • 4
  • -
  • 6
  • -
  • 12
  • -
  • 24
  • -
  • ...
  • -
-
- -
-
-
-
+ + <% include preloadCSS %> + + + +
+
+

+

Loading the client

+
+
+
+
+
+
+
+
+ <%- include('partials/toolbar') %> + +
+ +
+
+
+ - -
-
- -
- Settings -
-
Units
-
-
-
-
-
Date format
-
-
-
-
-
Language
-
- -
-
-
-
Scale
-
- -
-
-
-
Render Basal
-
- -
-
-
-
Enable Alarms
-
-
-
-
-
- - - mins -
-
- - - mins -
-
-
-
-
Night Mode
-
-
-
-
Edit Mode
-
-
-
-
Show Raw BG Data
-
-
-
-
-
-
Custom Title
-
-
-
-
Theme
-
-
-
-
-
-
Show Plugins
-
-
- - - - <%- include('partials/authentication-status') %> - -
- About -
-
version
-
head
-

-

License: AGPL
-
Copyright © 2017 Nightscout contributors
-

- -
-
+ +
+
+
+ +
+
+
---
+
+
- -
-
-
- Log a Treatment - - - -
- Targets - - -
- -
- Glucose Reading - - - - -
-
- - - -
- - - - - - - - - - -
- - - -
- Event Time - - - - -
- - -
-
+
+
+ +
    +
  • Hours:
  • +
  • 2
  • +
  • 3
  • +
  • 4
  • +
  • 6
  • +
  • 12
  • +
  • 24
  • +
  • ...
  • +
+
+ +
+
+
+
+
+ +
+
+ +
+ Settings +
+
Units
+
+
+
+
+
Date format
+
+
+
+
+
Language
+
+ +
+
+
+
Scale
+
+ +
+
+
+
Render Basal
+
+ +
+
+
+
Enable Alarms
+
+
+
+
+
+ + + mins +
+
+ + + mins +
+
+
+
+
Night Mode
+
+
+
+
Edit Mode
+
+
+
+
Show Raw BG Data
+
+
+
+
+
+
Custom Title
+
+
+
+
Theme
+
+
+
+
+
+
Show Plugins
+
+
+ + + + <%- include('partials/authentication-status') %> + +
+ About +
+
version
+
head
+

+

License: AGPL
+
Copyright © 2017 Nightscout contributors
+

+ +
+
+
+ +
+
+
+ Log a Treatment + + + +
+ Targets + + +
+ +
+ Glucose Reading + + + + +
+
+ + + +
+ + + + + + + + + + +
+ + + +
+ Event Time + + + +
-
- -
- - Bolus Wizard - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - BG: - - - - -
- - - - - - - - - - 0.00 -
- Quickpick: -
- -
- - or - - Add from database - - -
-
-
- - - Carbs: - - g - - - -
- - - COB: - - g - - - -
- - - IOB: - - 0.00 -
- - Other correction: - - -
- - Rounding: - - 0.00 -
- - Calculation is in target range. -
- - - Insulin needed: - - 0.00 -
- - Carbs needed: - - - -
- - Basal rate: - - -
-
- + +
+ +
+ +
+
+
+ + Bolus Wizard + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + BG: + + + + +
+ + + + + + + + + + 0.00 +
+ Quickpick:
- +
- - - - -
- - - - -
- Event Time: - - - - - - + + or + + Add from database + + +
- - - - - - +
+ + + Carbs: + + g + + + +
+ + + COB: + + g + + + +
+ + + IOB: + + 0.00 +
+ + Other correction: + + +
+ + Rounding: + + 0.00 +
+ + Calculation is in target range. +
+ + + Insulin needed: + + 0.00 +
+ + Carbs needed: + + + +
+ + Basal rate: + + +
+
+ +
+ +
+ + + + +
+ + + + +
+ Event Time: + + + + + +
+ +
+ + + + +
+ +
+ + +
+ + + + + + -
- - -
- - - > - - - - + - diff --git a/views/service-worker.js b/views/service-worker.js index ae6b20edf75..3815cafe378 100644 --- a/views/service-worker.js +++ b/views/service-worker.js @@ -69,33 +69,26 @@ function update(request) { // On install, cache some resources. self.addEventListener('install', function(evt) { - console.log('The service worker is being installed.'); - - // Ask the service worker to keep installing until the returning promise - // resolves. + //console.log('The service worker is being installed.'); evt.waitUntil(precache()); }); function inCache(request) { - for (var i = 0; i < CACHE_LIST.length; i++) { - if (request.url.endsWith(CACHE_LIST[i])) { - return true; - } + let found = false; + CACHE_LIST.forEach( function (e) { + if (request.url.endsWith(e)) { + found = true; } - return false; + }); + return found; } -// On fetch, use cache but update the entry with the latest contents -// from the server. self.addEventListener('fetch', function(evt) { - if (!evt.request.url.startsWith(self.location.origin) || CACHE == 'developmentMode' || !inCache(evt.request) || evt.request.method !== 'GET') { - console.log('Skipping cache for ', evt.request.url); + //console.log('Skipping cache for ', evt.request.url); return void evt.respondWith(fetch(evt.request)); } - - console.log('Returning cached for ', evt.request.url); - + //console.log('Returning cached for ', evt.request.url); evt.respondWith(fromCache(evt.request)); evt.waitUntil(update(evt.request)); }); @@ -105,7 +98,7 @@ self.addEventListener('activate', event => { caches.keys().then(cacheNames => { return cacheNames.filter(cacheName => CACHE !== cacheName); }).then(unusedCaches => { - console.log('DESTROYING CACHE', unusedCaches.join(',')); + //console.log('DESTROYING CACHE', unusedCaches.join(',')); return Promise.all(unusedCaches.map(unusedCache => { return caches.delete(unusedCache); })); From 443ebb53048a04d5939d9b7848142cd9f0d7bcd8 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Tue, 4 Feb 2020 15:39:47 +0200 Subject: [PATCH 4/5] Make Codacy happy --- views/service-worker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/views/service-worker.js b/views/service-worker.js index 3815cafe378..4404b3ff785 100644 --- a/views/service-worker.js +++ b/views/service-worker.js @@ -84,7 +84,7 @@ function inCache(request) { } self.addEventListener('fetch', function(evt) { - if (!evt.request.url.startsWith(self.location.origin) || CACHE == 'developmentMode' || !inCache(evt.request) || evt.request.method !== 'GET') { + if (!evt.request.url.startsWith(self.location.origin) || CACHE === 'developmentMode' || !inCache(evt.request) || evt.request.method !== 'GET') { //console.log('Skipping cache for ', evt.request.url); return void evt.respondWith(fetch(evt.request)); } @@ -93,11 +93,11 @@ self.addEventListener('fetch', function(evt) { evt.waitUntil(update(evt.request)); }); -self.addEventListener('activate', event => { +self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then(cacheNames => { - return cacheNames.filter(cacheName => CACHE !== cacheName); - }).then(unusedCaches => { + return cacheNames.filter((cacheName) => CACHE !== cacheName); + }).then((unusedCaches) => { //console.log('DESTROYING CACHE', unusedCaches.join(',')); return Promise.all(unusedCaches.map(unusedCache => { return caches.delete(unusedCache); From a221e370bc243b786f3bf6ad3ba5c8415a47e899 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Tue, 4 Feb 2020 15:56:58 +0200 Subject: [PATCH 5/5] More parentheses --- views/service-worker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/views/service-worker.js b/views/service-worker.js index 4404b3ff785..50e720b1aba 100644 --- a/views/service-worker.js +++ b/views/service-worker.js @@ -95,11 +95,11 @@ self.addEventListener('fetch', function(evt) { self.addEventListener('activate', (event) => { event.waitUntil( - caches.keys().then(cacheNames => { + caches.keys().then((cacheNames) => { return cacheNames.filter((cacheName) => CACHE !== cacheName); }).then((unusedCaches) => { //console.log('DESTROYING CACHE', unusedCaches.join(',')); - return Promise.all(unusedCaches.map(unusedCache => { + return Promise.all(unusedCaches.map((unusedCache) => { return caches.delete(unusedCache); })); }).then(() => self.clients.claim())