-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is behind the `minify` flag, which is enabled by default. This will minify all HTML and CSS (but not JS due to an upstream bug) at build-time and request-time. This reduced the size of the development initial HTML load for the `core/basic` example by 20%.
- Loading branch information
1 parent
2309c5c
commit 50e04e0
Showing
6 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::errors::*; | ||
use minify_html_onepass::{with_friendly_error, Cfg}; | ||
|
||
/// Minifies the given HTML document, including any CSS and JS. This is | ||
/// *extremely* fast, and can be reasonably run before returning a request. | ||
/// | ||
/// If the second argument is set to `false`, CSS and JS will not be minified, | ||
/// and the performance will be improved. | ||
pub(crate) fn minify(code: &str, minify_extras: bool) -> Result<String, ServerError> { | ||
// In case the user is using invalid HTML (very tricky error to track down), we | ||
// let them disable this feature | ||
if cfg!(feature = "minify") { | ||
let cfg = Cfg { | ||
minify_js: false, // Pending wilsonzlin/minify-js#1 | ||
minify_css: minify_extras, | ||
}; | ||
let mut bytes = code.as_bytes().to_vec(); | ||
|
||
match with_friendly_error(&mut bytes, &cfg) { | ||
Ok(min_len) => Ok(std::str::from_utf8(&bytes[..min_len]).unwrap().to_string()), | ||
Err(err) => Err(ServerError::MinifyError { | ||
// We have to wrap this because the error types are non-`StdError` | ||
// We want the error to be nice for the user (and it's not a security risk, since | ||
// the HTML is being sent to the client anyway, if this is being run on the server) | ||
source: std::io::Error::new(std::io::ErrorKind::NotFound, format!("{:#?}", err)), | ||
}), | ||
} | ||
} else { | ||
Ok(code.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters