Skip to content

Commit 8de4945

Browse files
authored
Merge pull request #1862 from Zentrik/brotli
Add brotli compression of static assets
2 parents c61ebd9 + f7dce8e commit 8de4945

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
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: 17 additions & 2 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(Vec<u8>),
33+
}
34+
2935
pub struct ResourceResolver {
3036
tera: RwLock<Tera>,
3137
}
@@ -39,10 +45,19 @@ impl ResourceResolver {
3945
})
4046
}
4147

42-
pub fn get_static_asset(&self, path: &str) -> Option<Vec<u8>> {
48+
pub fn get_static_asset(&self, path: &str, use_compression: bool) -> Option<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 Some(Payload::Compressed(data));
55+
}
56+
}
57+
4358
StaticCompiledAssets::get(path)
4459
.or_else(|| StaticAssets::get(path))
45-
.map(|file| file.data.to_vec())
60+
.map(|file| Payload::Uncompressed(file.data.to_vec()))
4661
}
4762

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

site/src/server.rs

Lines changed: 17 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);

0 commit comments

Comments
 (0)