Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GZip the static content #384

Merged
merged 2 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gui
Submodule gui updated 3 files
+11,631 −6,422 package-lock.json
+13 −12 package.json
+20 −40 webpack.config.js
5 changes: 3 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ board_build.partitions = ${common.build_partitions}
# ; use a special branch
# framework-arduinoespressif32 @ https://github.com/marcovannoord/arduino-esp32.git#idf-release/v4.0
# platformio/framework-arduinoespressif32 @ ~3.10006.0
monitor_flags =
--filter=esp32_exception_decoder
monitor_filters = esp32_exception_decoder

[env:openevse_nodemcu-32s]
board = nodemcu-32s
Expand Down Expand Up @@ -265,6 +264,8 @@ build_flags =
-D TX2=32
board_build.extra_flags = "-DARDUINO_ESP32_GATEWAY=\'E\'"
upload_speed = 921600
monitor_flags =
--filter=esp32_exception_decoder

[env:openevse_esp32-heltec-wifi-lora-v2]
board = heltec_wifi_lora_32_V2
Expand Down
21 changes: 12 additions & 9 deletions scripts/extra_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def make_static(env, target, source):

out_files = []
for file in listdir(dist_dir):
if isfile(join(dist_dir, file)):
if isfile(join(dist_dir, file)) and (file.endswith(".gz") or file.endswith(".png") or file.endswith(".jpg")):
out_files.append(file)

# Sort files to make sure the order is constant
Expand All @@ -84,24 +84,27 @@ def make_static(env, target, source):
output += "StaticFile staticFiles[] = {\n"

for out_file in out_files:
filetype = "TEXT"
if out_file.endswith(".css"):
filetype = None
compress = True
if out_file.endswith(".css.gz"):
filetype = "CSS"
elif out_file.endswith(".js"):
elif out_file.endswith(".js.gz"):
filetype = "JS"
elif out_file.endswith(".htm") or out_file.endswith(".html"):
elif out_file.endswith(".htm.gz") or out_file.endswith(".html.gz"):
filetype = "HTML"
elif out_file.endswith(".jpg"):
filetype = "JPEG"
compress = False
elif out_file.endswith(".png"):
filetype = "PNG"
elif out_file.endswith(".svg"):
compress = False
elif out_file.endswith(".svg.gz"):
filetype = "SVG"
elif out_file.endswith(".json"):
elif out_file.endswith(".json.gz"):
filetype = "JSON"

c_name = get_c_name(out_file)
output += " { \"/"+out_file+"\", CONTENT_"+c_name+", sizeof(CONTENT_"+c_name+") - 1, _CONTENT_TYPE_"+filetype+", CONTENT_"+c_name+"_ETAG },\n"
output += " { \"/"+out_file.replace(".gz","")+"\", CONTENT_"+c_name+", sizeof(CONTENT_"+c_name+") - 1, _CONTENT_TYPE_"+filetype+", CONTENT_"+c_name+"_ETAG, "+("true" if compress else "false")+" },\n"

output += "};\n"

Expand All @@ -115,7 +118,7 @@ def process_html_app(source, dest, env):
web_server_static = join("$BUILDSRC_DIR", "web_server_static.cpp.o")

for file in sorted(listdir(source)):
if isfile(join(source, file)):
if isfile(join(source, file)) and (file.endswith(".gz") or file.endswith(".png") or file.endswith(".jpg")):
data_file = join(source, file)
header_file = join(dest, "web_server."+file+".h")
env.Command(header_file, data_file, data_to_header)
Expand Down
4 changes: 4 additions & 0 deletions src/web_server_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct StaticFile
size_t length;
const char *type;
const char *etag;
bool compressed;
};

#include "web_static/web_server_static_files.h"
Expand Down Expand Up @@ -100,6 +101,9 @@ bool web_static_handle(MongooseHttpServerRequest *request)
if (enableCors) {
response->addHeader(F("Access-Control-Allow-Origin"), F("*"));
}
if(file->compressed) {
response->addHeader(F("Content-Encoding"), F("gzip"));
}

response->addHeader("Etag", file->etag);
response->setContent((const uint8_t *)file->data, file->length);
Expand Down
3 changes: 0 additions & 3 deletions src/web_static/web_server.assets.js.h

This file was deleted.

Loading