using rocket instead of astro

This commit is contained in:
Philippe Loctaux 2023-12-01 10:54:31 +01:00
parent eb72400722
commit e61ef1d4c3
79 changed files with 4406 additions and 8501 deletions

39
src/minify.rs Normal file
View file

@ -0,0 +1,39 @@
use rocket::fairing::{self, Fairing, Kind};
use rocket::http::ContentType;
use rocket::{Request, Response};
pub struct Minify;
#[rocket::async_trait]
impl Fairing for Minify {
fn info(&self) -> fairing::Info {
fairing::Info {
name: "Minify HTML",
kind: Kind::Response,
}
}
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
if response.content_type() == Some(ContentType::HTML) {
let body = response.body_mut();
if let Ok(original) = body.to_bytes().await {
let cfg = minify_html::Cfg {
// Be HTML spec compliant
do_not_minify_doctype: true,
ensure_spec_compliant_unquoted_attribute_values: true,
keep_spaces_between_attributes: true,
// The rest
keep_closing_tags: true,
keep_html_and_head_opening_tags: true,
minify_css: false,
minify_js: true,
..Default::default()
};
let minified = minify_html::minify(&original, &cfg);
response.set_sized_body(minified.len(), std::io::Cursor::new(minified));
}
}
}
}