use gzip
This commit is contained in:
parent
455bcc8474
commit
57b96e8d61
5 changed files with 15 additions and 400 deletions
|
@ -15,7 +15,7 @@ set(TARGET_SRCS
|
||||||
httplib.h
|
httplib.h
|
||||||
)
|
)
|
||||||
set(PUBLIC_ASSETS
|
set(PUBLIC_ASSETS
|
||||||
index.html
|
index.html.gz
|
||||||
loading.html
|
loading.html
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
BIN
examples/server/public/index.html.gz
Normal file
BIN
examples/server/public/index.html.gz
Normal file
Binary file not shown.
|
@ -15,7 +15,7 @@
|
||||||
#define MIMETYPE_JSON "application/json; charset=utf-8"
|
#define MIMETYPE_JSON "application/json; charset=utf-8"
|
||||||
|
|
||||||
// auto generated files (update with ./deps.sh)
|
// auto generated files (update with ./deps.sh)
|
||||||
#include "index.html.hpp"
|
#include "index.html.gz.hpp"
|
||||||
#include "loading.html.hpp"
|
#include "loading.html.hpp"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
@ -3828,8 +3828,13 @@ int main(int argc, char ** argv) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// using embedded static index.html
|
// using embedded static index.html
|
||||||
svr->Get("/", [](const httplib::Request &, httplib::Response & res) {
|
svr->Get("/", [](const httplib::Request & req, httplib::Response & res) {
|
||||||
res.set_content(reinterpret_cast<const char*>(index_html), index_html_len, "text/html; charset=utf-8");
|
if (req.get_header_value("Accept-Encoding").find("gzip") == std::string::npos) {
|
||||||
|
res.set_content("Error: gzip is not supported by this browser", "text/plain");
|
||||||
|
} else {
|
||||||
|
res.set_header("Content-Encoding", "gzip");
|
||||||
|
res.set_content(reinterpret_cast<const char*>(index_html_gz), index_html_gz_len, "text/html; charset=utf-8");
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import { viteSingleFile } from 'vite-plugin-singlefile';
|
import { viteSingleFile } from 'vite-plugin-singlefile';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import zlib from 'zlib';
|
||||||
|
|
||||||
const MAX_BUNDLE_SIZE = 1024 * 1024; // only increase when absolutely necessary
|
const MAX_BUNDLE_SIZE = 1024 * 1024; // only increase when absolutely necessary
|
||||||
|
|
||||||
|
@ -26,17 +27,18 @@ const BUILD_PLUGINS = [
|
||||||
},
|
},
|
||||||
writeBundle() {
|
writeBundle() {
|
||||||
const outputIndexHtml = path.join(config.build.outDir, 'index.html');
|
const outputIndexHtml = path.join(config.build.outDir, 'index.html');
|
||||||
const content = fs.readFileSync(outputIndexHtml, 'utf-8');
|
const content = GUIDE_FOR_FRONTEND + '\n' + fs.readFileSync(outputIndexHtml, 'utf-8');
|
||||||
|
const compressed = zlib.gzipSync(Buffer.from(content, 'utf-8'), { level: 9 });
|
||||||
|
|
||||||
if (content.length > MAX_BUNDLE_SIZE) {
|
if (compressed.byteLength > MAX_BUNDLE_SIZE) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Bundle size is too large (${Math.ceil(content.length / 1024)} KB).\n` +
|
`Bundle size is too large (${Math.ceil(content.length / 1024)} KB).\n` +
|
||||||
`Please reduce the size of the frontend or increase MAX_BUNDLE_SIZE in vite.config.js.\n`,
|
`Please reduce the size of the frontend or increase MAX_BUNDLE_SIZE in vite.config.js.\n`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetOutputFile = path.join(config.build.outDir, '../../public/index.html');
|
const targetOutputFile = path.join(config.build.outDir, '../../public/index.html.gz');
|
||||||
fs.writeFileSync(targetOutputFile, GUIDE_FOR_FRONTEND + '\n' + content);
|
fs.writeFileSync(targetOutputFile, compressed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})(),
|
})(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue