Only compress web assets in the CLI (#2329)
* only compress web assets in the CLI * precompressed -> pre-compressed * only enable pre-compression in release builds
This commit is contained in:
1 parent
b147f5a737
commit
a27d4e71ed
13 files changed
+461
-468
No files matched your search
@@ -18,7 +18,6 @@ dioxus_server_macro = { workspace = true }
|
||||
# axum
|
||||
axum = { workspace = true, features = ["ws", "macros"], optional = true }
|
||||
tower-http = { workspace = true, optional = true, features = ["fs"] }
|
||||
async-compression = { version = "0.4.6", features = ["gzip", "tokio"], optional = true }
|
||||
|
||||
dioxus-lib = { workspace = true }
|
||||
|
||||
@@ -77,7 +76,7 @@ desktop = ["dioxus-desktop", "server_fn/reqwest", "dioxus_server_macro/reqwest"]
|
||||
mobile = ["dioxus-mobile"]
|
||||
default-tls = ["server_fn/default-tls"]
|
||||
rustls = ["server_fn/rustls"]
|
||||
axum = ["dep:axum", "tower-http", "server", "server_fn/axum", "dioxus_server_macro/axum", "async-compression"]
|
||||
axum = ["dep:axum", "tower-http", "server", "server_fn/axum", "dioxus_server_macro/axum"]
|
||||
server = [
|
||||
"server_fn/ssr",
|
||||
"dioxus_server_macro/server",
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
//! Handles pre-compression for any static assets
|
||||
|
||||
use std::{ffi::OsString, path::PathBuf, pin::Pin};
|
||||
|
||||
use async_compression::tokio::bufread::GzipEncoder;
|
||||
use futures_util::Future;
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
#[allow(unused)]
|
||||
pub async fn pre_compress_files(directory: PathBuf) -> tokio::io::Result<()> {
|
||||
// print to stdin encoded gzip data
|
||||
pre_compress_dir(directory).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pre_compress_dir(
|
||||
path: PathBuf,
|
||||
) -> Pin<Box<dyn Future<Output = tokio::io::Result<()>> + Send + Sync>> {
|
||||
Box::pin(async move {
|
||||
let mut entries = tokio::fs::read_dir(&path).await?;
|
||||
let mut set: JoinSet<tokio::io::Result<()>> = JoinSet::new();
|
||||
|
||||
while let Some(entry) = entries.next_entry().await? {
|
||||
set.spawn(async move {
|
||||
if entry.file_type().await?.is_dir() {
|
||||
if let Err(err) = pre_compress_dir(entry.path()).await {
|
||||
tracing::error!(
|
||||
"Failed to pre-compress directory {}: {}",
|
||||
entry.path().display(),
|
||||
err
|
||||
);
|
||||
}
|
||||
} else if let Err(err) = pre_compress_file(entry.path()).await {
|
||||
tracing::error!(
|
||||
"Failed to pre-compress static assets {}: {}",
|
||||
entry.path().display(),
|
||||
err
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
while let Some(res) = set.join_next().await {
|
||||
res??;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
async fn pre_compress_file(path: PathBuf) -> tokio::io::Result<()> {
|
||||
let file = tokio::fs::File::open(&path).await?;
|
||||
let stream = tokio::io::BufReader::new(file);
|
||||
let mut encoder = GzipEncoder::new(stream);
|
||||
let new_extension = match path.extension() {
|
||||
Some(ext) => {
|
||||
if ext.to_string_lossy().to_lowercase().ends_with("gz") {
|
||||
return Ok(());
|
||||
}
|
||||
let mut ext = ext.to_os_string();
|
||||
ext.push(".gz");
|
||||
ext
|
||||
}
|
||||
None => OsString::from("gz"),
|
||||
};
|
||||
let output = path.with_extension(new_extension);
|
||||
let mut buffer = tokio::fs::File::create(&output).await?;
|
||||
tokio::io::copy(&mut encoder, &mut buffer).await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -207,6 +207,8 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
// TODO: This is a breaking change, but we should probably serve static assets from a different directory than dist where the server executable is located
|
||||
// This would prevent issues like https://github.com/DioxusLabs/dioxus/issues/2327
|
||||
fn serve_static_assets(
|
||||
mut self,
|
||||
assets_path: impl Into<std::path::PathBuf>,
|
||||
@@ -215,11 +217,6 @@ where
|
||||
|
||||
let assets_path = assets_path.into();
|
||||
async move {
|
||||
#[cfg(not(debug_assertions))]
|
||||
if let Err(err) = crate::assets::pre_compress_files(assets_path.clone()).await {
|
||||
tracing::error!("Failed to pre-compress static assets: {}", err);
|
||||
}
|
||||
|
||||
// Serve all files in dist folder except index.html
|
||||
let dir = std::fs::read_dir(&assets_path).unwrap_or_else(|e| {
|
||||
panic!(
|
||||
@@ -246,9 +243,9 @@ where
|
||||
.join("/");
|
||||
let route = format!("/{}", route);
|
||||
if path.is_dir() {
|
||||
self = self.nest_service(&route, ServeDir::new(path).precompressed_gzip());
|
||||
self = self.nest_service(&route, ServeDir::new(path).precompressed_br());
|
||||
} else {
|
||||
self = self.nest_service(&route, ServeFile::new(path).precompressed_gzip());
|
||||
self = self.nest_service(&route, ServeFile::new(path).precompressed_br());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,6 @@ pub use once_cell;
|
||||
|
||||
mod html_storage;
|
||||
|
||||
#[cfg(feature = "axum")]
|
||||
mod assets;
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "axum")))]
|
||||
#[cfg(feature = "axum")]
|
||||
mod axum_adapter;
|
||||
|
||||
Reference in new issue
Block a user