From 109272df595b8d80c8301d92a734c0a369e11bae Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 17 Oct 2024 21:29:31 +0200 Subject: Add max-age cache control for matrix well-known files --- webserver.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/webserver.js b/webserver.js index 39f2410..a6d9c6f 100755 --- a/webserver.js +++ b/webserver.js @@ -198,7 +198,12 @@ function anyComponentHidden(fname) { // if 'mime' is not null/undefined, it should be the content-type of the file // options: -// { mime: "content-type value", listdirs: true/false, cors: true/false } +// { +// mime: "content-type value", +// listdirs: true/false, +// cors: true/false, +// cache: "cache-control value" +// } // (defaults are null/false) function requestFile(req, res, path, origpath, options) { if (options == null) options = {}; @@ -213,6 +218,7 @@ function requestFile(req, res, path, origpath, options) { const headers = {}; if (options.mime != null) headers["content-type"] = options.mime; if (options.cors == true) headers["access-control-allow-origin"] = "*"; + if (options.cache != null) headers["cache-control"] = options.cache; res.sendFile(fname, {headers}); } else if (stats.isDirectory() && options.listdirs) { const items = fs @@ -277,7 +283,9 @@ app.get(["/ff", "/ff/*"], function (req, res) { }); app.get("/.well-known/matrix/*", function (req, res) { - requestFile(req, res, "/well-known" + unescape(makeUrlSafe(req, 12)), req.url, {mime: "application/json", cors: true}); + // Prevent these matrix files from being requested for every trifling event + // by giving them an explicit cache lifetime + requestFile(req, res, "/well-known" + unescape(makeUrlSafe(req, 12)), req.url, {mime: "application/json", cors: true, cache: "public, max-age=7200"}); }); app.get("/.well-known/*", function (req, res) { -- cgit v1.2.3-70-g09d2