mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-11 18:54:28 +05:00
refactor: improved web cache
This commit is contained in:
12
gulpfile.js
12
gulpfile.js
@@ -60,6 +60,10 @@ const styles = (cb) => {
|
|||||||
const items = paths.styles.bundles[name];
|
const items = paths.styles.bundles[name];
|
||||||
|
|
||||||
src(items)
|
src(items)
|
||||||
|
.pipe(replace(
|
||||||
|
"{BUILD_TIME}",
|
||||||
|
Math.floor(Date.now() / 1000)
|
||||||
|
))
|
||||||
.pipe(postcss([
|
.pipe(postcss([
|
||||||
cssnano({ preset: 'advanced' })
|
cssnano({ preset: 'advanced' })
|
||||||
]))
|
]))
|
||||||
@@ -78,6 +82,10 @@ const scripts = (cb) => {
|
|||||||
const items = paths.scripts.bundles[name];
|
const items = paths.scripts.bundles[name];
|
||||||
|
|
||||||
src(items)
|
src(items)
|
||||||
|
.pipe(replace(
|
||||||
|
"{BUILD_TIME}",
|
||||||
|
Math.floor(Date.now() / 1000)
|
||||||
|
))
|
||||||
.pipe(terser().on('error', console.error))
|
.pipe(terser().on('error', console.error))
|
||||||
.pipe(concat(name))
|
.pipe(concat(name))
|
||||||
.pipe(gzip({
|
.pipe(gzip({
|
||||||
@@ -94,6 +102,10 @@ const jsonFiles = (cb) => {
|
|||||||
const item = paths.json[i];
|
const item = paths.json[i];
|
||||||
|
|
||||||
src(item.src)
|
src(item.src)
|
||||||
|
.pipe(replace(
|
||||||
|
"{BUILD_TIME}",
|
||||||
|
Math.floor(Date.now() / 1000)
|
||||||
|
))
|
||||||
.pipe(jsonminify())
|
.pipe(jsonminify())
|
||||||
.pipe(gzip({
|
.pipe(gzip({
|
||||||
append: true
|
append: true
|
||||||
|
|||||||
@@ -218,7 +218,6 @@ protected:
|
|||||||
CanHandleCallback canHandleCallback;
|
CanHandleCallback canHandleCallback;
|
||||||
BeforeSendCallback beforeSendCallback;
|
BeforeSendCallback beforeSendCallback;
|
||||||
TemplateCallback templateCallback;
|
TemplateCallback templateCallback;
|
||||||
String eTag;
|
|
||||||
const char* uri = nullptr;
|
const char* uri = nullptr;
|
||||||
const char* path = nullptr;
|
const char* path = nullptr;
|
||||||
const char* cacheHeader = nullptr;
|
const char* cacheHeader = nullptr;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
#include <detail/mimetable.h>
|
#include <detail/mimetable.h>
|
||||||
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
|
#include <detail/RequestHandlersImpl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace mime;
|
using namespace mime;
|
||||||
|
|
||||||
@@ -47,21 +50,25 @@ public:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
|
||||||
if (server._eTagEnabled) {
|
if (server._eTagEnabled) {
|
||||||
if (server._eTagFunction) {
|
if (this->eTag.isEmpty()) {
|
||||||
this->eTag = (server._eTagFunction)(*this->fs, this->path);
|
if (server._eTagFunction) {
|
||||||
|
this->eTag = (server._eTagFunction)(*this->fs, this->path);
|
||||||
|
|
||||||
} else if (this->eTag.isEmpty()) {
|
} else {
|
||||||
this->eTag = esp8266webserver::calcETag(*this->fs, this->path);
|
#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);
|
server.send(304);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!this->path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !this->fs->exists(path)) {
|
if (!this->path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !this->fs->exists(path)) {
|
||||||
String pathWithGz = this->path + FPSTR(mimeTable[gz].endsWith);
|
String pathWithGz = this->path + FPSTR(mimeTable[gz].endsWith);
|
||||||
@@ -84,11 +91,11 @@ public:
|
|||||||
server.sendHeader(F("Cache-Control"), this->cacheHeader);
|
server.sendHeader(F("Cache-Control"), this->cacheHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
if (server._eTagEnabled && !this->eTag.isEmpty()) {
|
||||||
if (server._eTagEnabled && this->eTag.length() > 0) {
|
|
||||||
server.sendHeader(F("ETag"), this->eTag);
|
server.sendHeader(F("ETag"), this->eTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
server.streamFile(file, F("text/html"), method);
|
server.streamFile(file, F("text/html"), method);
|
||||||
#else
|
#else
|
||||||
server.streamFile(file, F("text/html"), 200);
|
server.streamFile(file, F("text/html"), 200);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#define PORTAL_CACHE_TIME "max-age=86400"
|
//#define PORTAL_CACHE "max-age=86400"
|
||||||
#define PORTAL_CACHE (settings.system.logLevel >= TinyLogger::Level::TRACE ? nullptr : PORTAL_CACHE_TIME)
|
#define PORTAL_CACHE nullptr
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
#include <Updater.h>
|
#include <Updater.h>
|
||||||
@@ -72,9 +72,24 @@ protected:
|
|||||||
void setup() {
|
void setup() {
|
||||||
this->dnsServer->setTTL(0);
|
this->dnsServer->setTTL(0);
|
||||||
this->dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
this->dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
this->webServer->enableETag(true, [](FS &fs, const String &fName) -> const String {
|
||||||
this->webServer->enableETag(true);
|
char buf[32];
|
||||||
#endif
|
{
|
||||||
|
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
|
// index page
|
||||||
/*auto indexPage = (new DynamicPage("/", &LittleFS, "/pages/index.html"))
|
/*auto indexPage = (new DynamicPage("/", &LittleFS, "/pages/index.html"))
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class Lang {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchTranslations(locale) {
|
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();
|
const data = await response.json();
|
||||||
|
|
||||||
if (data.values instanceof Object) {
|
if (data.values instanceof Object) {
|
||||||
|
|||||||
Reference in New Issue
Block a user