Skip to content

Commit eaad923

Browse files
committed
Add brotli compression of static files.
200kb reduction in compare.js size from 277kb to 77kb.
1 parent 11a3c71 commit eaad923

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

site/frontend/.parcelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@parcel/config-default",
3+
"compressors": {
4+
"*.{css,js}": [
5+
"...",
6+
"@parcel/compressor-brotli"
7+
]
8+
}
9+
}

site/frontend/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

site/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"license": "MIT",
1313
"devDependencies": {
1414
"@babel/types": "^7.21.4",
15+
"@parcel/compressor-brotli": "^2.8.3",
1516
"@parcel/transformer-vue": "^2.8.3",
1617
"@types/highcharts": "^7.0.0",
1718
"@types/msgpack-lite": "^0.1.8",

site/src/resources.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct StaticAssets;
1717
#[derive(RustEmbed)]
1818
#[folder = "frontend/dist"]
1919
#[include = "*.js"]
20+
#[include = "*.br"]
2021
#[include = "*.css"]
2122
struct StaticCompiledAssets;
2223

@@ -26,6 +27,11 @@ struct StaticCompiledAssets;
2627
#[include = "*.html"]
2728
struct TemplateAssets;
2829

30+
pub enum Payload {
31+
Compressed(Vec<u8>),
32+
Uncompressed(Option<Vec<u8>>),
33+
}
34+
2935
pub struct ResourceResolver {
3036
tera: RwLock<Tera>,
3137
}
@@ -39,10 +45,21 @@ impl ResourceResolver {
3945
})
4046
}
4147

42-
pub fn get_static_asset(&self, path: &str) -> Option<Vec<u8>> {
43-
StaticCompiledAssets::get(path)
44-
.or_else(|| StaticAssets::get(path))
45-
.map(|file| file.data.to_vec())
48+
pub fn get_static_asset(&self, path: &str, use_compression: bool) -> Payload {
49+
if use_compression {
50+
let compressed_path = path.to_owned() + ".br";
51+
let data =
52+
StaticCompiledAssets::get(compressed_path.as_str()).map(|file| file.data.to_vec());
53+
if let Some(data) = data {
54+
return Payload::Compressed(data);
55+
}
56+
}
57+
58+
Payload::Uncompressed(
59+
StaticCompiledAssets::get(path)
60+
.or_else(|| StaticAssets::get(path))
61+
.map(|file| file.data.to_vec()),
62+
)
4663
}
4764

4865
pub async fn get_template(&self, path: &str) -> anyhow::Result<Vec<u8>> {

site/src/server.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use crate::api::{
2727
use crate::db::{self, ArtifactId};
2828
use crate::load::{Config, SiteCtxt};
2929
use crate::request_handlers;
30-
use crate::resources::ResourceResolver;
30+
use crate::resources::{Payload, ResourceResolver};
3131

3232
pub type Request = http::Request<hyper::Body>;
3333
pub type Response = http::Response<hyper::Body>;
@@ -357,7 +357,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
357357
None
358358
};
359359

360-
if let Some(response) = handle_fs_path(&req, path).await {
360+
if let Some(response) = handle_fs_path(&req, path, allow_compression).await {
361361
return Ok(response);
362362
}
363363

@@ -594,7 +594,11 @@ lazy_static::lazy_static! {
594594
}
595595

596596
/// Handle the case where the path is to a static file
597-
async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Body>> {
597+
async fn handle_fs_path(
598+
req: &Request,
599+
path: &str,
600+
use_compression: bool,
601+
) -> Option<http::Response<hyper::Body>> {
598602
if path.contains("./") | path.contains("../") {
599603
return Some(not_found());
600604
}
@@ -627,7 +631,16 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
627631
| "/detailed-query.html"
628632
| "/help.html"
629633
| "/status.html" => resolve_template(relative_path).await,
630-
_ => TEMPLATES.get_static_asset(relative_path)?,
634+
_ => match TEMPLATES.get_static_asset(relative_path, use_compression) {
635+
Payload::Compressed(data) => {
636+
response = response.header(
637+
hyper::header::CONTENT_ENCODING,
638+
hyper::header::HeaderValue::from_static("br"),
639+
);
640+
data
641+
}
642+
Payload::Uncompressed(data) => data?,
643+
},
631644
};
632645

633646
let p = Path::new(&path);
@@ -638,6 +651,12 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
638651
Some("svg") => response = response.header("Content-Type", "image/svg+xml"),
639652
Some("css") => response = response.header("Content-Type", "text/css"),
640653
Some("js") => response = response.header("Content-Type", "application/javascript"),
654+
Some("br") => {
655+
response = response.header(
656+
hyper::header::CONTENT_ENCODING,
657+
hyper::header::HeaderValue::from_static("br"),
658+
)
659+
}
641660
_ => {
642661
if path.is_empty() || path == "/" {
643662
response = response.header_typed(ContentType::html());

0 commit comments

Comments
 (0)