Skip to content

Add brotli compression of static assets #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions site/frontend/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@parcel/config-default",
"compressors": {
"*.{css,js}": [
"...",
"@parcel/compressor-brotli"
]
}
}
18 changes: 18 additions & 0 deletions site/frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions site/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "MIT",
"devDependencies": {
"@babel/types": "^7.21.4",
"@parcel/compressor-brotli": "^2.8.3",
"@parcel/transformer-vue": "^2.8.3",
"@types/highcharts": "^7.0.0",
"@types/msgpack-lite": "^0.1.8",
Expand Down
19 changes: 17 additions & 2 deletions site/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct StaticAssets;
#[derive(RustEmbed)]
#[folder = "frontend/dist"]
#[include = "*.js"]
#[include = "*.br"]
#[include = "*.css"]
struct StaticCompiledAssets;

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

pub enum Payload {
Compressed(Vec<u8>),
Uncompressed(Vec<u8>),
}

pub struct ResourceResolver {
tera: RwLock<Tera>,
}
Expand All @@ -39,10 +45,19 @@ impl ResourceResolver {
})
}

pub fn get_static_asset(&self, path: &str) -> Option<Vec<u8>> {
pub fn get_static_asset(&self, path: &str, use_compression: bool) -> Option<Payload> {
if use_compression {
let compressed_path = path.to_owned() + ".br";
let data =
StaticCompiledAssets::get(compressed_path.as_str()).map(|file| file.data.to_vec());
if let Some(data) = data {
return Some(Payload::Compressed(data));
}
}

StaticCompiledAssets::get(path)
.or_else(|| StaticAssets::get(path))
.map(|file| file.data.to_vec())
.map(|file| Payload::Uncompressed(file.data.to_vec()))
}

pub async fn get_template(&self, path: &str) -> anyhow::Result<Vec<u8>> {
Expand Down
21 changes: 17 additions & 4 deletions site/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use crate::api::{
use crate::db::{self, ArtifactId};
use crate::load::{Config, SiteCtxt};
use crate::request_handlers;
use crate::resources::ResourceResolver;
use crate::resources::{Payload, ResourceResolver};

pub type Request = http::Request<hyper::Body>;
pub type Response = http::Response<hyper::Body>;
Expand Down Expand Up @@ -357,7 +357,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
None
};

if let Some(response) = handle_fs_path(&req, path).await {
if let Some(response) = handle_fs_path(&req, path, allow_compression).await {
return Ok(response);
}

Expand Down Expand Up @@ -594,7 +594,11 @@ lazy_static::lazy_static! {
}

/// Handle the case where the path is to a static file
async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hyper::Body>> {
async fn handle_fs_path(
req: &Request,
path: &str,
use_compression: bool,
) -> Option<http::Response<hyper::Body>> {
if path.contains("./") | path.contains("../") {
return Some(not_found());
}
Expand Down Expand Up @@ -627,7 +631,16 @@ async fn handle_fs_path(req: &Request, path: &str) -> Option<http::Response<hype
| "/detailed-query.html"
| "/help.html"
| "/status.html" => resolve_template(relative_path).await,
_ => TEMPLATES.get_static_asset(relative_path)?,
_ => match TEMPLATES.get_static_asset(relative_path, use_compression)? {
Payload::Compressed(data) => {
response = response.header(
hyper::header::CONTENT_ENCODING,
hyper::header::HeaderValue::from_static("br"),
);
data
}
Payload::Uncompressed(data) => data,
},
};

let p = Path::new(&path);
Expand Down