Pre-compress index JS/CSS with gzip

This commit is contained in:
david-beinder 2023-07-06 00:38:46 +02:00
parent 84fe477783
commit 76f09b306a
No known key found for this signature in database
3 changed files with 20 additions and 8 deletions

View File

@ -1,10 +1,12 @@
static const char HEADER_CACHE_CONTROL[] PROGMEM = "Cache-Control";
static const char HEADER_CONTENT_ENCODING[] PROGMEM = "Content-Encoding";
static const char HEADER_PRAGMA[] PROGMEM = "Pragma";
static const char HEADER_EXPIRES[] PROGMEM = "Expires";
static const char HEADER_AUTHENTICATE[] PROGMEM = "WWW-Authenticate";
static const char HEADER_LOCATION[] PROGMEM = "Location";
static const char CACHE_CONTROL_NO_CACHE[] PROGMEM = "no-cache, no-store, must-revalidate";
static const char CONTENT_ENCODING_GZIP[] PROGMEM = "gzip";
static const char CACHE_1HR[] PROGMEM = "public, max-age=3600";
static const char CACHE_1MO[] PROGMEM = "public, max-age=2592000";
static const char PRAGMA_NO_CACHE[] PROGMEM = "no-cache";

View File

@ -2,6 +2,7 @@ import os
import re
import shutil
import subprocess
import gzip
try:
from css_html_js_minify import html_minify, js_minify, css_minify
@ -65,16 +66,24 @@ for webroot in ["lib/SvelteUi/app/dist", "lib/SvelteUi/json"]:
content = js_minify(content)
except:
print("WARN: Unable to minify")
content_bytes = content.encode("utf-8")
if filename in ["index.js", "index.css"]:
content_bytes = gzip.compress(content_bytes, compresslevel=9)
content_len = len(content_bytes)
else:
content_len = len(content_bytes)
content_bytes += b"\0"
with open(dstfile, "w") as dst:
dst.write("static const char ")
dst.write(varname)
dst.write("[] PROGMEM = R\"==\"==(")
dst.write(content)
dst.write(")==\"==\";\n")
dst.write("[] PROGMEM = {")
dst.write(", ".join([str(c) for c in content_bytes]))
dst.write("};\n")
dst.write("const int ");
dst.write(varname)
dst.write("_LEN PROGMEM = ");
dst.write(str(len(content)))
dst.write(str(content_len))
dst.write(";");

View File

@ -765,8 +765,8 @@ void AmsWebServer::indexCss() {
return;
server.sendHeader(HEADER_CACHE_CONTROL, CACHE_1MO);
server.setContentLength(INDEX_CSS_LEN);
server.send_P(200, MIME_CSS, INDEX_CSS);
server.sendHeader(HEADER_CONTENT_ENCODING, CONTENT_ENCODING_GZIP);
server.send_P(200, MIME_CSS, INDEX_CSS, INDEX_CSS_LEN);
}
void AmsWebServer::indexJs() {
@ -776,7 +776,8 @@ void AmsWebServer::indexJs() {
return;
server.sendHeader(HEADER_CACHE_CONTROL, CACHE_1MO);
server.send_P(200, MIME_JS, INDEX_JS);
server.sendHeader(HEADER_CONTENT_ENCODING, CONTENT_ENCODING_GZIP);
server.send_P(200, MIME_JS, INDEX_JS, INDEX_JS_LEN);
}
void AmsWebServer::configurationJson() {