mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 08:28:12 +01:00
Use gzip compression for the web server component's static resources (#5291)
Co-authored-by: Daniel Dunn <dannydunn@eternityforest.com>
This commit is contained in:
parent
f14419bab5
commit
19d53c6643
3 changed files with 15 additions and 2 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import gzip
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.components import web_server_base
|
from esphome.components import web_server_base
|
||||||
|
@ -109,9 +110,13 @@ def build_index_html(config) -> str:
|
||||||
return html
|
return html
|
||||||
|
|
||||||
|
|
||||||
def add_resource_as_progmem(resource_name: str, content: str) -> None:
|
def add_resource_as_progmem(
|
||||||
|
resource_name: str, content: str, compress: bool = True
|
||||||
|
) -> None:
|
||||||
"""Add a resource to progmem."""
|
"""Add a resource to progmem."""
|
||||||
content_encoded = content.encode("utf-8")
|
content_encoded = content.encode("utf-8")
|
||||||
|
if compress:
|
||||||
|
content_encoded = gzip.compress(content_encoded)
|
||||||
content_encoded_size = len(content_encoded)
|
content_encoded_size = len(content_encoded)
|
||||||
bytes_as_int = ", ".join(str(x) for x in content_encoded)
|
bytes_as_int = ", ".join(str(x) for x in content_encoded)
|
||||||
uint8_t = f"const uint8_t ESPHOME_WEBSERVER_{resource_name}[{content_encoded_size}] PROGMEM = {{{bytes_as_int}}}"
|
uint8_t = f"const uint8_t ESPHOME_WEBSERVER_{resource_name}[{content_encoded_size}] PROGMEM = {{{bytes_as_int}}}"
|
||||||
|
@ -137,7 +142,8 @@ async def to_code(config):
|
||||||
cg.add_define("USE_WEBSERVER_PORT", config[CONF_PORT])
|
cg.add_define("USE_WEBSERVER_PORT", config[CONF_PORT])
|
||||||
cg.add_define("USE_WEBSERVER_VERSION", version)
|
cg.add_define("USE_WEBSERVER_VERSION", version)
|
||||||
if version == 2:
|
if version == 2:
|
||||||
add_resource_as_progmem("INDEX_HTML", build_index_html(config))
|
# Don't compress the index HTML as the data sizes are almost the same.
|
||||||
|
add_resource_as_progmem("INDEX_HTML", build_index_html(config), compress=False)
|
||||||
else:
|
else:
|
||||||
cg.add(var.set_css_url(config[CONF_CSS_URL]))
|
cg.add(var.set_css_url(config[CONF_CSS_URL]))
|
||||||
cg.add(var.set_js_url(config[CONF_JS_URL]))
|
cg.add(var.set_js_url(config[CONF_JS_URL]))
|
||||||
|
|
|
@ -328,6 +328,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||||
void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||||
AsyncWebServerResponse *response =
|
AsyncWebServerResponse *response =
|
||||||
request->beginResponse_P(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
request->beginResponse_P(200, "text/html", ESPHOME_WEBSERVER_INDEX_HTML, ESPHOME_WEBSERVER_INDEX_HTML_SIZE);
|
||||||
|
// No gzip header here because the HTML file is so small
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -336,6 +337,7 @@ void WebServer::handle_index_request(AsyncWebServerRequest *request) {
|
||||||
void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
||||||
AsyncWebServerResponse *response =
|
AsyncWebServerResponse *response =
|
||||||
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
request->beginResponse_P(200, "text/css", ESPHOME_WEBSERVER_CSS_INCLUDE, ESPHOME_WEBSERVER_CSS_INCLUDE_SIZE);
|
||||||
|
response->addHeader("Content-Encoding", "gzip");
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -344,6 +346,7 @@ void WebServer::handle_css_request(AsyncWebServerRequest *request) {
|
||||||
void WebServer::handle_js_request(AsyncWebServerRequest *request) {
|
void WebServer::handle_js_request(AsyncWebServerRequest *request) {
|
||||||
AsyncWebServerResponse *response =
|
AsyncWebServerResponse *response =
|
||||||
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
request->beginResponse_P(200, "text/javascript", ESPHOME_WEBSERVER_JS_INCLUDE, ESPHOME_WEBSERVER_JS_INCLUDE_SIZE);
|
||||||
|
response->addHeader("Content-Encoding", "gzip");
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,6 +21,10 @@ wifi:
|
||||||
ssid: "MySSID"
|
ssid: "MySSID"
|
||||||
password: "password1"
|
password: "password1"
|
||||||
|
|
||||||
|
web_server:
|
||||||
|
port: 80
|
||||||
|
version: 2
|
||||||
|
|
||||||
i2c:
|
i2c:
|
||||||
sda: 4
|
sda: 4
|
||||||
scl: 5
|
scl: 5
|
||||||
|
|
Loading…
Reference in a new issue