From 85c4c09811a11be887af551b7c6de7c30c823ae0 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Fri, 1 Nov 2024 15:02:23 -0700 Subject: [PATCH] feat: enable ssg using `/api/static_routes` (#3157) * feat: enable ssg using `/api/static_routes` --- packages/cli/src/builder/bundle.rs | 73 ++++++++++++++++++++++++++++ packages/cli/src/tracer.rs | 11 ++++- packages/interpreter/src/js/hash.txt | 2 +- 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/builder/bundle.rs b/packages/cli/src/builder/bundle.rs index a2e47a1dc..c30ab8348 100644 --- a/packages/cli/src/builder/bundle.rs +++ b/packages/cli/src/builder/bundle.rs @@ -287,6 +287,8 @@ impl AppBundle { /// /// It's not guaranteed that they're different from any other folder fn prepare_build_dir(&self) -> Result<()> { + _ = std::fs::remove_dir_all(&self.app_dir()); + create_dir_all(self.app_dir())?; create_dir_all(self.exe_dir())?; create_dir_all(self.asset_dir())?; @@ -579,6 +581,11 @@ impl AppBundle { }) .await .unwrap()?; + + // Run SSG and cache static routes + if self.build.build.ssg { + self.run_ssg().await?; + } } Platform::MacOS => {} Platform::Windows => {} @@ -719,4 +726,70 @@ impl AppBundle { Ok(()) } + + async fn run_ssg(&self) -> anyhow::Result<()> { + use futures_util::stream::FuturesUnordered; + use futures_util::StreamExt; + use tokio::process::Command; + + const PORT: u16 = 9999; + + tracing::info!("Running SSG"); + + // Run the server executable + let _child = Command::new( + self.server_exe() + .context("Failed to find server executable")?, + ) + .env(dioxus_cli_config::SERVER_PORT_ENV, PORT.to_string()) + .env(dioxus_cli_config::SERVER_IP_ENV, "127.0.0.1".to_string()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .kill_on_drop(true) + .spawn()?; + + // Wait a second for the server to start + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + + // Get the routes from the `/static_routes` endpoint + let mut routes = reqwest::Client::builder() + .build()? + .post(format!("http://127.0.0.1:{PORT}/api/static_routes")) + .send() + .await + .context("Failed to get static routes from server")? + .text() + .await + .map(|raw| serde_json::from_str::(&raw).unwrap()) + .inspect(|text| tracing::debug!("Got static routes: {text:?}")) + .context("Failed to parse static routes from server")? + .lines() + .map(|line| line.to_string()) + .map(|line| async move { + tracing::info!("SSG: {line}"); + reqwest::Client::builder() + .build()? + .get(format!("http://127.0.0.1:{PORT}{line}")) + .header("Accept", "text/html") + .send() + .await + }) + .collect::>(); + + while let Some(route) = routes.next().await { + match route { + Ok(route) => tracing::debug!("ssg success: {route:?}"), + Err(err) => tracing::error!("ssg error: {err:?}"), + } + } + + // Wait a second for the cache to be written by the server + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + + tracing::info!("SSG complete"); + + drop(_child); + + Ok(()) + } } diff --git a/packages/cli/src/tracer.rs b/packages/cli/src/tracer.rs index be2e121da..0090034e8 100644 --- a/packages/cli/src/tracer.rs +++ b/packages/cli/src/tracer.rs @@ -71,8 +71,7 @@ impl TraceController { /// Build tracing infrastructure. pub fn initialize() { - let mut filter = - EnvFilter::new("error,dx=trace,dioxus-cli=debug,manganis-cli-support=debug"); + let mut filter = EnvFilter::new("error,dx=info,dioxus-cli=info,manganis-cli-support=info"); if env::var(LOG_ENV).is_ok() { filter = EnvFilter::from_env(LOG_ENV); @@ -270,10 +269,18 @@ struct FmtLogWriter {} impl Write for FmtLogWriter { fn write(&mut self, buf: &[u8]) -> io::Result { + if !TUI_ENABLED.load(Ordering::SeqCst) { + std::io::stdout().write(buf)?; + } + Ok(buf.len()) } fn flush(&mut self) -> io::Result<()> { + if !TUI_ENABLED.load(Ordering::SeqCst) { + std::io::stdout().flush()?; + } + Ok(()) } } diff --git a/packages/interpreter/src/js/hash.txt b/packages/interpreter/src/js/hash.txt index 931b219f9..a0f881b93 100644 --- a/packages/interpreter/src/js/hash.txt +++ b/packages/interpreter/src/js/hash.txt @@ -1 +1 @@ -[6449103750905854967, 4461869229701639737, 13069001215487072322, 8716623267269178440, 5336385715226370016, 14456089431355876478, 10411167459769688501, 5052021921702764563, 17534315583914394253, 5638004933879392817] \ No newline at end of file +[6449103750905854967, 4461869229701639737, 13069001215487072322, 8716623267269178440, 5336385715226370016, 14456089431355876478, 7422899642446454418, 5052021921702764563, 17534315583914394253, 5638004933879392817] \ No newline at end of file