From cb2d7b99f7dcb4a1e7ce6a58e2d9cb8863e1015c Mon Sep 17 00:00:00 2001 From: Bobby Date: Tue, 28 Jun 2022 13:38:55 +0700 Subject: [PATCH] fix: cache-control was set to all frontend pages this now properly sets only on the specific pages that deserves to be cached in cdn additionally update sample config file to remove warning about cacheControl option this is now mature enough to be simply toggled on/off depending on your own necessity --- config.sample.js | 12 +++++++----- lolisafe.js | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/config.sample.js b/config.sample.js index 378921f..032719f 100644 --- a/config.sample.js +++ b/config.sample.js @@ -708,11 +708,13 @@ module.exports = { }, /* - ADVANCED: Use safe.fiery.me-exclusive cache control. - This will only work properly with certain settings in nginx reverse proxy and Cloudflare. - Do NOT enable unless you know what you are doing. - true: With CDN (Cloudflare) - 2: When NOT using Cloudflare + Enable Cache-Control header tags. + Please consult the relevant codes in lolisafe.js to learn the specifics. + true or 1: Cloudflare (will cache some frontend pages in CDN) + 2: Basic Cache-Control without CDNs + + NOTE: If set to Cloudflare, and auth is specified in "cloudflare" option above, + lolisafe will automatically call Cloudflare API to purge cache of the relevant frontend pages. */ cacheControl: false, diff --git a/lolisafe.js b/lolisafe.js index 9bd46e1..8170cae 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -183,15 +183,18 @@ if (config.cacheControl) { next() }) - // If using CDN, cache public pages in CDN - if (config.cacheControl !== 2) { - cdnPages.push('api/check') - for (const page of cdnPages) { - safe.use(`/${page === 'home' ? '' : page}`, (req, res, next) => { - res.set('Cache-Control', cacheControls.cdn) - next() - }) - } + switch (config.cacheControl) { + case 1: + case true: + // If using CDN, cache public pages in CDN + cdnPages.push('api/check') + for (const page of cdnPages) { + safe.get(`/${page === 'home' ? '' : page}`, (req, res, next) => { + res.set('Cache-Control', cacheControls.cdn) + next() + }) + } + break } // If serving uploads with node