Skip to content

Commit 57b96e8

Browse files
committed
use gzip
1 parent 455bcc8 commit 57b96e8

File tree

5 files changed

+15
-400
lines changed

5 files changed

+15
-400
lines changed

examples/server/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set(TARGET_SRCS
1515
httplib.h
1616
)
1717
set(PUBLIC_ASSETS
18-
index.html
18+
index.html.gz
1919
loading.html
2020
)
2121

examples/server/public/index.html

Lines changed: 0 additions & 392 deletions
This file was deleted.
173 KB
Binary file not shown.

examples/server/server.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define MIMETYPE_JSON "application/json; charset=utf-8"
1616

1717
// auto generated files (update with ./deps.sh)
18-
#include "index.html.hpp"
18+
#include "index.html.gz.hpp"
1919
#include "loading.html.hpp"
2020

2121
#include <atomic>
@@ -3828,8 +3828,13 @@ int main(int argc, char ** argv) {
38283828
}
38293829
} else {
38303830
// using embedded static index.html
3831-
svr->Get("/", [](const httplib::Request &, httplib::Response & res) {
3832-
res.set_content(reinterpret_cast<const char*>(index_html), index_html_len, "text/html; charset=utf-8");
3831+
svr->Get("/", [](const httplib::Request & req, httplib::Response & res) {
3832+
if (req.get_header_value("Accept-Encoding").find("gzip") == std::string::npos) {
3833+
res.set_content("Error: gzip is not supported by this browser", "text/plain");
3834+
} else {
3835+
res.set_header("Content-Encoding", "gzip");
3836+
res.set_content(reinterpret_cast<const char*>(index_html_gz), index_html_gz_len, "text/html; charset=utf-8");
3837+
}
38333838
return false;
38343839
});
38353840
}

examples/server/webui/vite.config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { viteSingleFile } from 'vite-plugin-singlefile';
33
import path from 'path';
44
import fs from 'fs';
5+
import zlib from 'zlib';
56

67
const MAX_BUNDLE_SIZE = 1024 * 1024; // only increase when absolutely necessary
78

@@ -26,17 +27,18 @@ const BUILD_PLUGINS = [
2627
},
2728
writeBundle() {
2829
const outputIndexHtml = path.join(config.build.outDir, 'index.html');
29-
const content = fs.readFileSync(outputIndexHtml, 'utf-8');
30+
const content = GUIDE_FOR_FRONTEND + '\n' + fs.readFileSync(outputIndexHtml, 'utf-8');
31+
const compressed = zlib.gzipSync(Buffer.from(content, 'utf-8'), { level: 9 });
3032

31-
if (content.length > MAX_BUNDLE_SIZE) {
33+
if (compressed.byteLength > MAX_BUNDLE_SIZE) {
3234
throw new Error(
3335
`Bundle size is too large (${Math.ceil(content.length / 1024)} KB).\n` +
3436
`Please reduce the size of the frontend or increase MAX_BUNDLE_SIZE in vite.config.js.\n`,
3537
);
3638
}
3739

38-
const targetOutputFile = path.join(config.build.outDir, '../../public/index.html');
39-
fs.writeFileSync(targetOutputFile, GUIDE_FOR_FRONTEND + '\n' + content);
40+
const targetOutputFile = path.join(config.build.outDir, '../../public/index.html.gz');
41+
fs.writeFileSync(targetOutputFile, compressed);
4042
}
4143
}
4244
})(),

0 commit comments

Comments
 (0)