Merge pull request #1704 from Moustachauve/content-cache

Add Cache-Control to index
This commit is contained in:
Aircoookie 2021-01-31 00:42:24 +01:00 committed by GitHub
commit 94941a7732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -229,14 +229,32 @@ void serveIndexOrWelcome(AsyncWebServerRequest *request)
}
}
bool handleIfNoneMatchCacheHeader(AsyncWebServerRequest* request)
{
AsyncWebHeader* header = request->getHeader("If-None-Match");
if (header && header->value() == String(VERSION)) {
request->send(304);
return true;
}
return false;
}
bool setStaticContentCacheHeaders(AsyncWebServerResponse *response)
{
response->addHeader(F("Cache-Control"),"max-age=2592000");
response->addHeader(F("ETag"), String(VERSION));
}
void serveIndex(AsyncWebServerRequest* request)
{
if (handleFileRead(request, "/index.htm")) return;
if (handleIfNoneMatchCacheHeader(request)) return;
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L);
response->addHeader(F("Content-Encoding"),"gzip");
setStaticContentCacheHeaders(response);
request->send(response);
}