Skip to content

Commit

Permalink
Move app caching to a service worker (#5504)
Browse files Browse the repository at this point in the history
* Move app caching to a webworker

* Code cleanup

* Code cleanup

* Make Codacy happy

* More parentheses
  • Loading branch information
sulkaharo authored Feb 5, 2020
1 parent c0f6b22 commit 82f0076
Show file tree
Hide file tree
Showing 9 changed files with 844 additions and 750 deletions.
16 changes: 10 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions views/adminindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<meta name="theme-color" content="#333333">


<link rel="preload" href="/css/main.css?v=<%= locals.cachebuster %>" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/main.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/drawer.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/admin.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/ui-darkness/jquery-ui.min.css?v=<%= locals.cachebuster %>" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/ui-darkness/jquery-ui.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<% include preloadCSS %>
</head>
<body>
Expand All @@ -39,8 +39,8 @@

<%- include('partials/authentication-status') %>

<script src="<%= locals.bundle %>/js/bundle.app.js?v=<%= locals.cachebuster %>"></script>
<script src="/socket.io/socket.io.js?v=<%= locals.cachebuster %>"></script>
<script src="/admin/js/admin.js?v=<%= locals.cachebuster %>"></script>
<script src="<%= locals.bundle %>/js/bundle.app.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/admin/js/admin.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion views/clockviews/shared.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<div id="staleTime"></div>
</div>
</main>
<script src="<%= locals.bundle %>/js/bundle.clock.js?v=<%= locals.cachebuster %>"></script>
<script src="<%= locals.bundle %>/js/bundle.clock.js"></script>

<script type="text/javascript">
var clockFace = "<%= face %>"; // can now be used in scripts
Expand Down
10 changes: 5 additions & 5 deletions views/foodindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<meta name="msapplication-config" content="/browserconfig.xml">
<meta name="theme-color" content="#333333">

<link rel="preload" href="/css/main.css?v=<%= locals.cachebuster %>" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/main.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/dropdown.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/drawer.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/food.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/ui-darkness/jquery-ui.min.css?v=<%= locals.cachebuster %>" as="style" onload="this.onload=null;this.rel='stylesheet'">
<link rel="preload" href="/css/ui-darkness/jquery-ui.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<% include preloadCSS %>

</head>
Expand Down Expand Up @@ -112,8 +112,8 @@

<%- include('partials/authentication-status') %>

<script src="<%= locals.bundle %>/js/bundle.app.js?v=<%= locals.cachebuster %>"></script>
<script src="/socket.io/socket.io.js?v=<%= locals.cachebuster %>"></script>
<script src="/food/js/food.js?v=<%= locals.cachebuster %>"></script>
<script src="<%= locals.bundle %>/js/bundle.app.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/food/js/food.js"></script>
</body>
</html>
Loading

0 comments on commit 82f0076

Please sign in to comment.