provide a nicer builder API

This commit is contained in:
Evan Almloff committed 2023-04-01 17:00:09 -05:00
1 parent 5ffdb4dbed
commit 71ddd50963
6 files changed
+64 -43

No files matched your search

+5 -23
View File
@@ -16,38 +16,20 @@ fn dioxus_ssr_html<P: 'static + Clone>(cfg: &ServeConfig<P>, renderer: &mut Rend
..
} = cfg;
let application_name = application_name.unwrap_or("dioxus");
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
let _ = vdom.rebuild();
let base_path = base_path.unwrap_or(".");
let mut html = String::new();
let result = match head {
Some(head) => {
write!(
&mut html,
r#"
let result = write!(
&mut html,
r#"
<!DOCTYPE html>
<html>
<head>{head}
</head>
<body>
</head><body>
<div id="main">"#
)
}
None => {
write!(
&mut html,
r#"<title>Dioxus Application</title>
<link rel="preload" href="/{base_path}/assets/dioxus/{application_name}_bg.wasm" as="fetch" type="application/wasm" crossorigin="" />
<link rel="modulepreload" href="/{base_path}/assets/dioxus/{application_name}.js" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />"#
)
}
};
);
if let Err(err) = result {
eprintln!("Failed to write to html: {}", err);
+45 -2
View File
@@ -1,15 +1,16 @@
use dioxus_core::Component;
#[derive(Clone)]
pub struct ServeConfig<P: Clone> {
pub struct ServeConfigBuilder<P: Clone> {
pub(crate) app: Component<P>,
pub(crate) props: P,
pub(crate) application_name: Option<&'static str>,
pub(crate) base_path: Option<&'static str>,
pub(crate) head: Option<&'static str>,
pub(crate) assets_path: Option<&'static str>,
}
impl<P: Clone> ServeConfig<P> {
impl<P: Clone> ServeConfigBuilder<P> {
/// Create a new ServeConfig
pub fn new(app: Component<P>, props: P) -> Self {
Self {
@@ -18,6 +19,7 @@ impl<P: Clone> ServeConfig<P> {
application_name: None,
base_path: None,
head: None,
assets_path: None,
}
}
@@ -38,4 +40,45 @@ impl<P: Clone> ServeConfig<P> {
self.head = Some(head);
self
}
/// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist/assets)
pub fn assets_path(mut self, assets_path: &'static str) -> Self {
self.assets_path = Some(assets_path);
self
}
/// Build the ServeConfig
pub fn build(self) -> ServeConfig<P> {
let base_path = self.base_path.unwrap_or(".");
let application_name = self.application_name.unwrap_or("dioxus");
ServeConfig {
app: self.app,
props: self.props,
application_name,
base_path,
head: self.head.map(String::from).unwrap_or(format!(r#"<title>Dioxus Application</title>
<link rel="preload" href="/{base_path}/assets/dioxus/{application_name}_bg.wasm" as="fetch" type="application/wasm" crossorigin="" />
<link rel="modulepreload" href="/{base_path}/assets/dioxus/{application_name}.js" />
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />"#)),
assets_path: self.assets_path.unwrap_or("dist/assets"),
}
}
}
#[derive(Clone)]
pub struct ServeConfig<P: Clone> {
pub(crate) app: Component<P>,
pub(crate) props: P,
pub(crate) application_name: &'static str,
pub(crate) base_path: &'static str,
pub(crate) head: String,
pub(crate) assets_path: &'static str,
}
impl<P: Clone> From<ServeConfigBuilder<P>> for ServeConfig<P> {
fn from(builder: ServeConfigBuilder<P>) -> Self {
builder.build()
}
}