mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-29 07:30:20 +00:00
feat: enable ssg using /api/static_routes
(#3157)
* feat: enable ssg using `/api/static_routes`
This commit is contained in:
parent
bb0de9a55a
commit
85c4c09811
3 changed files with 83 additions and 3 deletions
|
@ -287,6 +287,8 @@ impl AppBundle {
|
||||||
///
|
///
|
||||||
/// It's not guaranteed that they're different from any other folder
|
/// It's not guaranteed that they're different from any other folder
|
||||||
fn prepare_build_dir(&self) -> Result<()> {
|
fn prepare_build_dir(&self) -> Result<()> {
|
||||||
|
_ = std::fs::remove_dir_all(&self.app_dir());
|
||||||
|
|
||||||
create_dir_all(self.app_dir())?;
|
create_dir_all(self.app_dir())?;
|
||||||
create_dir_all(self.exe_dir())?;
|
create_dir_all(self.exe_dir())?;
|
||||||
create_dir_all(self.asset_dir())?;
|
create_dir_all(self.asset_dir())?;
|
||||||
|
@ -579,6 +581,11 @@ impl AppBundle {
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap()?;
|
.unwrap()?;
|
||||||
|
|
||||||
|
// Run SSG and cache static routes
|
||||||
|
if self.build.build.ssg {
|
||||||
|
self.run_ssg().await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Platform::MacOS => {}
|
Platform::MacOS => {}
|
||||||
Platform::Windows => {}
|
Platform::Windows => {}
|
||||||
|
@ -719,4 +726,70 @@ impl AppBundle {
|
||||||
|
|
||||||
Ok(())
|
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::<String>(&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::<FuturesUnordered<_>>();
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,8 +71,7 @@ impl TraceController {
|
||||||
|
|
||||||
/// Build tracing infrastructure.
|
/// Build tracing infrastructure.
|
||||||
pub fn initialize() {
|
pub fn initialize() {
|
||||||
let mut filter =
|
let mut filter = EnvFilter::new("error,dx=info,dioxus-cli=info,manganis-cli-support=info");
|
||||||
EnvFilter::new("error,dx=trace,dioxus-cli=debug,manganis-cli-support=debug");
|
|
||||||
|
|
||||||
if env::var(LOG_ENV).is_ok() {
|
if env::var(LOG_ENV).is_ok() {
|
||||||
filter = EnvFilter::from_env(LOG_ENV);
|
filter = EnvFilter::from_env(LOG_ENV);
|
||||||
|
@ -270,10 +269,18 @@ struct FmtLogWriter {}
|
||||||
|
|
||||||
impl Write for FmtLogWriter {
|
impl Write for FmtLogWriter {
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
|
if !TUI_ENABLED.load(Ordering::SeqCst) {
|
||||||
|
std::io::stdout().write(buf)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> io::Result<()> {
|
fn flush(&mut self) -> io::Result<()> {
|
||||||
|
if !TUI_ENABLED.load(Ordering::SeqCst) {
|
||||||
|
std::io::stdout().flush()?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
[6449103750905854967, 4461869229701639737, 13069001215487072322, 8716623267269178440, 5336385715226370016, 14456089431355876478, 10411167459769688501, 5052021921702764563, 17534315583914394253, 5638004933879392817]
|
[6449103750905854967, 4461869229701639737, 13069001215487072322, 8716623267269178440, 5336385715226370016, 14456089431355876478, 7422899642446454418, 5052021921702764563, 17534315583914394253, 5638004933879392817]
|
Loading…
Reference in a new issue