|
@@ -1,12 +1,15 @@
|
|
|
|
+use std::fs::File;
|
|
|
|
+use std::io::Read;
|
|
|
|
+use std::path::PathBuf;
|
|
|
|
+
|
|
use dioxus_core::Component;
|
|
use dioxus_core::Component;
|
|
|
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
pub struct ServeConfigBuilder<P: Clone> {
|
|
pub struct ServeConfigBuilder<P: Clone> {
|
|
pub(crate) app: Component<P>,
|
|
pub(crate) app: Component<P>,
|
|
pub(crate) props: 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) root_id: Option<&'static str>,
|
|
|
|
+ pub(crate) index_path: Option<&'static str>,
|
|
pub(crate) assets_path: Option<&'static str>,
|
|
pub(crate) assets_path: Option<&'static str>,
|
|
}
|
|
}
|
|
|
|
|
|
@@ -16,32 +19,25 @@ impl<P: Clone> ServeConfigBuilder<P> {
|
|
Self {
|
|
Self {
|
|
app,
|
|
app,
|
|
props,
|
|
props,
|
|
- application_name: None,
|
|
|
|
- base_path: None,
|
|
|
|
- head: None,
|
|
|
|
|
|
+ root_id: None,
|
|
|
|
+ index_path: None,
|
|
assets_path: None,
|
|
assets_path: None,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /// Set the application name matching the name in the Dioxus.toml file used to build the application
|
|
|
|
- pub fn application_name(mut self, application_name: &'static str) -> Self {
|
|
|
|
- self.application_name = Some(application_name);
|
|
|
|
|
|
+ /// Set the path of the index.html file to be served. (defaults to {assets_path}/index.html)
|
|
|
|
+ pub fn index_path(mut self, index_path: &'static str) -> Self {
|
|
|
|
+ self.index_path = Some(index_path);
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
- /// Set the path the WASM application will be served under
|
|
|
|
- pub fn base_path(mut self, base_path: &'static str) -> Self {
|
|
|
|
- self.base_path = Some(base_path);
|
|
|
|
|
|
+ /// Set the id of the root element in the index.html file to place the prerendered content into. (defaults to main)
|
|
|
|
+ pub fn root_id(mut self, root_id: &'static str) -> Self {
|
|
|
|
+ self.root_id = Some(root_id);
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
- /// Set the head content to be included in the HTML document served
|
|
|
|
- pub fn head(mut self, head: &'static str) -> Self {
|
|
|
|
- self.head = Some(head);
|
|
|
|
- self
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist/assets)
|
|
|
|
|
|
+ /// Set the path of the assets folder generated by the Dioxus CLI. (defaults to dist)
|
|
pub fn assets_path(mut self, assets_path: &'static str) -> Self {
|
|
pub fn assets_path(mut self, assets_path: &'static str) -> Self {
|
|
self.assets_path = Some(assets_path);
|
|
self.assets_path = Some(assets_path);
|
|
self
|
|
self
|
|
@@ -49,31 +45,61 @@ impl<P: Clone> ServeConfigBuilder<P> {
|
|
|
|
|
|
/// Build the ServeConfig
|
|
/// Build the ServeConfig
|
|
pub fn build(self) -> ServeConfig<P> {
|
|
pub fn build(self) -> ServeConfig<P> {
|
|
- let base_path = self.base_path.unwrap_or(".");
|
|
|
|
- let application_name = self.application_name.unwrap_or("dioxus");
|
|
|
|
|
|
+ let assets_path = self.assets_path.unwrap_or("dist");
|
|
|
|
+
|
|
|
|
+ let index_path = self
|
|
|
|
+ .index_path
|
|
|
|
+ .map(PathBuf::from)
|
|
|
|
+ .unwrap_or_else(|| format!("{assets_path}/index.html").into());
|
|
|
|
+
|
|
|
|
+ let root_id = self.root_id.unwrap_or("main");
|
|
|
|
+
|
|
|
|
+ let index = load_index_html(index_path, root_id);
|
|
|
|
+
|
|
ServeConfig {
|
|
ServeConfig {
|
|
app: self.app,
|
|
app: self.app,
|
|
props: self.props,
|
|
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"),
|
|
|
|
|
|
+ index,
|
|
|
|
+ assets_path,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+fn load_index_html(path: PathBuf, root_id: &'static str) -> IndexHtml {
|
|
|
|
+ let mut file = File::open(path).expect("Failed to find index.html. Make sure the index_path is set correctly and the WASM application has been built.");
|
|
|
|
+
|
|
|
|
+ let mut contents = String::new();
|
|
|
|
+ file.read_to_string(&mut contents)
|
|
|
|
+ .expect("Failed to read index.html");
|
|
|
|
+
|
|
|
|
+ let (pre_main, post_main) = contents.split_once(&format!("id=\"{root_id}\"")).unwrap_or_else(|| panic!("Failed to find id=\"{root_id}\" in index.html. The id is used to inject the application into the page."));
|
|
|
|
+
|
|
|
|
+ let post_main = post_main.split_once('>').unwrap_or_else(|| {
|
|
|
|
+ panic!("Failed to find closing > after id=\"{root_id}\" in index.html.")
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let (pre_main, post_main) = (
|
|
|
|
+ pre_main.to_string() + &format!("id=\"{root_id}\"") + post_main.0 + ">",
|
|
|
|
+ post_main.1.to_string(),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ IndexHtml {
|
|
|
|
+ pre_main,
|
|
|
|
+ post_main,
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Clone)]
|
|
|
|
+pub(crate) struct IndexHtml {
|
|
|
|
+ pub(crate) pre_main: String,
|
|
|
|
+ pub(crate) post_main: String,
|
|
|
|
+}
|
|
|
|
+
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
pub struct ServeConfig<P: Clone> {
|
|
pub struct ServeConfig<P: Clone> {
|
|
pub(crate) app: Component<P>,
|
|
pub(crate) app: Component<P>,
|
|
pub(crate) props: P,
|
|
pub(crate) props: P,
|
|
- pub(crate) application_name: &'static str,
|
|
|
|
- pub(crate) base_path: &'static str,
|
|
|
|
- pub(crate) head: String,
|
|
|
|
|
|
+ pub(crate) index: IndexHtml,
|
|
pub(crate) assets_path: &'static str,
|
|
pub(crate) assets_path: &'static str,
|
|
}
|
|
}
|
|
|
|
|