File tree Expand file tree Collapse file tree 5 files changed +15
-400
lines changed Expand file tree Collapse file tree 5 files changed +15
-400
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ set(TARGET_SRCS
1515 httplib.h
1616)
1717set (PUBLIC_ASSETS
18- index.html
18+ index.html.gz
1919 loading.html
2020)
2121
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 22import { viteSingleFile } from 'vite-plugin-singlefile' ;
33import path from 'path' ;
44import fs from 'fs' ;
5+ import zlib from 'zlib' ;
56
67const 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 } ) ( ) ,
You can’t perform that action at this time.
0 commit comments