From 34eabca64ac3748b125137a929e806071ade7b3b Mon Sep 17 00:00:00 2001 From: Yurii Date: Thu, 14 Nov 2024 22:30:34 +0300 Subject: [PATCH] refactor: improved web cache --- gulpfile.js | 12 ++++++++++++ lib/WebServerHandlers/DynamicPage.h | 1 - lib/WebServerHandlers/StaticPage.h | 25 ++++++++++++++++--------- src/PortalTask.h | 25 ++++++++++++++++++++----- src_data/scripts/lang.js | 2 +- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index f0999ea..586b62f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -60,6 +60,10 @@ const styles = (cb) => { const items = paths.styles.bundles[name]; src(items) + .pipe(replace( + "{BUILD_TIME}", + Math.floor(Date.now() / 1000) + )) .pipe(postcss([ cssnano({ preset: 'advanced' }) ])) @@ -78,6 +82,10 @@ const scripts = (cb) => { const items = paths.scripts.bundles[name]; src(items) + .pipe(replace( + "{BUILD_TIME}", + Math.floor(Date.now() / 1000) + )) .pipe(terser().on('error', console.error)) .pipe(concat(name)) .pipe(gzip({ @@ -94,6 +102,10 @@ const jsonFiles = (cb) => { const item = paths.json[i]; src(item.src) + .pipe(replace( + "{BUILD_TIME}", + Math.floor(Date.now() / 1000) + )) .pipe(jsonminify()) .pipe(gzip({ append: true diff --git a/lib/WebServerHandlers/DynamicPage.h b/lib/WebServerHandlers/DynamicPage.h index 8553a0d..021f0f3 100644 --- a/lib/WebServerHandlers/DynamicPage.h +++ b/lib/WebServerHandlers/DynamicPage.h @@ -218,7 +218,6 @@ protected: CanHandleCallback canHandleCallback; BeforeSendCallback beforeSendCallback; TemplateCallback templateCallback; - String eTag; const char* uri = nullptr; const char* path = nullptr; const char* cacheHeader = nullptr; diff --git a/lib/WebServerHandlers/StaticPage.h b/lib/WebServerHandlers/StaticPage.h index 8b09721..14b643e 100644 --- a/lib/WebServerHandlers/StaticPage.h +++ b/lib/WebServerHandlers/StaticPage.h @@ -1,5 +1,8 @@ #include #include +#if defined(ARDUINO_ARCH_ESP32) + #include +#endif using namespace mime; @@ -47,21 +50,25 @@ public: return true; } - #if defined(ARDUINO_ARCH_ESP8266) if (server._eTagEnabled) { - if (server._eTagFunction) { - this->eTag = (server._eTagFunction)(*this->fs, this->path); + if (this->eTag.isEmpty()) { + if (server._eTagFunction) { + this->eTag = (server._eTagFunction)(*this->fs, this->path); - } else if (this->eTag.isEmpty()) { - this->eTag = esp8266webserver::calcETag(*this->fs, this->path); + } else { + #if defined(ARDUINO_ARCH_ESP8266) + this->eTag = esp8266webserver::calcETag(*this->fs, this->path); + #elif defined(ARDUINO_ARCH_ESP32) + this->eTag = StaticRequestHandler::calcETag(*this->fs, this->path); + #endif + } } - if (server.header(F("If-None-Match")).equals(this->eTag.c_str())) { + if (!this->eTag.isEmpty() && server.header(F("If-None-Match")).equals(this->eTag.c_str())) { server.send(304); return true; } } - #endif if (!this->path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !this->fs->exists(path)) { String pathWithGz = this->path + FPSTR(mimeTable[gz].endsWith); @@ -84,11 +91,11 @@ public: server.sendHeader(F("Cache-Control"), this->cacheHeader); } - #if defined(ARDUINO_ARCH_ESP8266) - if (server._eTagEnabled && this->eTag.length() > 0) { + if (server._eTagEnabled && !this->eTag.isEmpty()) { server.sendHeader(F("ETag"), this->eTag); } + #if defined(ARDUINO_ARCH_ESP8266) server.streamFile(file, F("text/html"), method); #else server.streamFile(file, F("text/html"), 200); diff --git a/src/PortalTask.h b/src/PortalTask.h index fd1a2b2..7e1358a 100644 --- a/src/PortalTask.h +++ b/src/PortalTask.h @@ -1,5 +1,5 @@ -#define PORTAL_CACHE_TIME "max-age=86400" -#define PORTAL_CACHE (settings.system.logLevel >= TinyLogger::Level::TRACE ? nullptr : PORTAL_CACHE_TIME) +//#define PORTAL_CACHE "max-age=86400" +#define PORTAL_CACHE nullptr #ifdef ARDUINO_ARCH_ESP8266 #include #include @@ -72,9 +72,24 @@ protected: void setup() { this->dnsServer->setTTL(0); this->dnsServer->setErrorReplyCode(DNSReplyCode::NoError); - #ifdef ARDUINO_ARCH_ESP8266 - this->webServer->enableETag(true); - #endif + this->webServer->enableETag(true, [](FS &fs, const String &fName) -> const String { + char buf[32]; + { + MD5Builder md5; + md5.begin(); + md5.add(fName); + md5.add(" " BUILD_ENV " " BUILD_VERSION " " __DATE__ " " __TIME__); + md5.calculate(); + md5.getChars(buf); + } + + String etag; + etag.reserve(34); + etag += '\"'; + etag.concat(buf, 32); + etag += '\"'; + return etag; + }); // index page /*auto indexPage = (new DynamicPage("/", &LittleFS, "/pages/index.html")) diff --git a/src_data/scripts/lang.js b/src_data/scripts/lang.js index 084f004..9d2e08b 100644 --- a/src_data/scripts/lang.js +++ b/src_data/scripts/lang.js @@ -63,7 +63,7 @@ class Lang { } async fetchTranslations(locale) { - const response = await fetch(`/static/locales/${locale}.json`); + const response = await fetch(`/static/locales/${locale}.json?{BUILD_TIME}`); const data = await response.json(); if (data.values instanceof Object) {