From 28df9069575a9f0f8e3ecbdd996d2186b572d1a3 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 6 Jun 2018 12:19:51 +0300 Subject: [PATCH] Implement caching for static resources --- client/service-worker.js | 3 ++- src/server.js | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/client/service-worker.js b/client/service-worker.js index 261550cc..94aa49dd 100644 --- a/client/service-worker.js +++ b/client/service-worker.js @@ -41,7 +41,8 @@ self.addEventListener("fetch", function(event) { function networkOrCache(uri) { return caches.open("thelounge").then(function(cache) { - return fetch(uri) + // 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() { diff --git a/src/server.js b/src/server.js index 1bfb60ba..a8abbf90 100644 --- a/src/server.js +++ b/src/server.js @@ -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