Merge pull request #2515 from thelounge/xpaw/transparent-sw-cache

Network-first service worker caches
This commit is contained in:
Jérémie Astori 2018-07-25 00:57:20 -04:00 committed by GitHub
commit 7b926f7c32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 6 deletions

View file

@ -44,7 +44,7 @@ audio:not([controls]) {
}
[hidden],
template {
display: none;
display: none !important;
}
a {
background: transparent;

View file

@ -60,4 +60,9 @@
};
window.addEventListener("error", window.g_LoungeErrorHandler);
// Trigger early service worker registration
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js");
}
})();

View file

@ -8,6 +8,18 @@ const webpush = require("../webpush");
const connect = $("#connect");
const utils = require("../utils");
window.addEventListener("beforeinstallprompt", (installPromptEvent) => {
$("#webapp-install-button")
.on("click", function() {
if (installPromptEvent && installPromptEvent.prompt) {
installPromptEvent.prompt();
}
$(this).prop("hidden", true);
})
.prop("hidden", false);
});
socket.on("configuration", function(data) {
if (options.initialized) {
// Likely a reconnect, request sync for possibly missed settings.

View file

@ -2,6 +2,62 @@
/* global clients */
"use strict";
const excludedPathsFromCache = /^(?:socket\.io|storage|uploads|cdn-cgi)\//;
self.addEventListener("install", function() {
self.skipWaiting();
});
self.addEventListener("activate", function(event) {
event.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", function(event) {
if (event.request.method !== "GET") {
return;
}
const url = event.request.url;
const scope = self.registration.scope;
// Skip cross-origin requests
if (!url.startsWith(scope)) {
return;
}
const path = url.substring(scope.length);
// Skip ignored paths
if (excludedPathsFromCache.test(path)) {
return;
}
const uri = new URL(url);
uri.hash = "";
uri.search = "";
event.respondWith(networkOrCache(uri));
});
function networkOrCache(uri) {
return caches.open("thelounge").then(function(cache) {
// Despite the "no-cache" name, it is a conditional request if proper headers are set
return fetch(uri, {cache: "no-cache"})
.then(function(response) {
if (response.ok) {
return cache.put(uri, response.clone()).then(function() {
return response;
});
}
})
.catch(function() {
return cache.match(uri).then(function(matching) {
return matching || Promise.reject("request-not-in-cache");
});
});
});
}
self.addEventListener("message", function(event) {
showNotification(event, event.data);
});

View file

@ -15,6 +15,8 @@
<div class="row">
<div class="col-sm-12">
<h2>Native app</h2>
<button type="button" class="btn" id="webapp-install-button" hidden>Add The Lounge to Home screen</button>
<button type="button" class="btn" id="make-default-client">Open irc:// URLs with The Lounge</button>
</div>

View file

@ -39,15 +39,17 @@ module.exports = function() {
(Node.js ${colors.green(process.versions.node)} on ${colors.green(process.platform)} ${process.arch})`);
log.info(`Configuration file: ${colors.green(Helper.getConfigPath())}`);
const staticOptions = {
redirect: false,
maxAge: 86400 * 1000,
};
const app = express()
.disable("x-powered-by")
.use(allRequests)
.use(index)
.use(express.static("public"))
.use("/storage/", express.static(Helper.getStoragePath(), {
redirect: false,
maxAge: 86400 * 1000,
}));
.use(express.static(path.join(__dirname, "..", "public"), staticOptions))
.use("/storage/", express.static(Helper.getStoragePath(), staticOptions));
// This route serves *installed themes only*. Local themes are served directly
// from the `public/themes/` folder as static assets, without entering this