Add Cache-Control to index

This commit is contained in:
Christophe Gagnier 2021-01-28 22:16:36 -05:00
parent 75c219d6c6
commit c24d574f90

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) void serveIndex(AsyncWebServerRequest* request)
{ {
if (handleFileRead(request, "/index.htm")) return; if (handleFileRead(request, "/index.htm")) return;
if (handleIfNoneMatchCacheHeader(request)) return;
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L); AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L);
response->addHeader(F("Content-Encoding"),"gzip"); response->addHeader(F("Content-Encoding"),"gzip");
setStaticContentCacheHeaders(response);
request->send(response); request->send(response);
} }