refactor: improved web cache

This commit is contained in:
Yurii
2024-11-14 22:30:34 +03:00
parent d3b28c5bfb
commit 34eabca64a
5 changed files with 49 additions and 16 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -1,5 +1,8 @@
#include <FS.h>
#include <detail/mimetable.h>
#if defined(ARDUINO_ARCH_ESP32)
#include <detail/RequestHandlersImpl.h>
#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);

View File

@@ -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 <ESP8266WebServer.h>
#include <Updater.h>
@@ -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"))

View File

@@ -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) {