collect templates for hot reloading
This commit is contained in:
1 parent
e1bb6cc750
commit
f96425e425
2 files changed
+75
-47
No files matched your search
@@ -6,6 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
# server functions
|
||||||
server_fn = { path = "D:/Users/Desktop/github/leptos/server_fn", features = ["stable"] }
|
server_fn = { path = "D:/Users/Desktop/github/leptos/server_fn", features = ["stable"] }
|
||||||
server_macro = { path = "server-macro" }
|
server_macro = { path = "server-macro" }
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ axum-macros = "0.3.7"
|
|||||||
salvo = { version = "0.37.7", optional = true, features = ["serve-static"] }
|
salvo = { version = "0.37.7", optional = true, features = ["serve-static"] }
|
||||||
serde = "1.0.159"
|
serde = "1.0.159"
|
||||||
|
|
||||||
|
# Dioxus + SSR
|
||||||
dioxus-core = { path = "../core", version = "^0.3.0" }
|
dioxus-core = { path = "../core", version = "^0.3.0" }
|
||||||
dioxus-ssr = { path = "../ssr", version = "^0.3.0", optional = true }
|
dioxus-ssr = { path = "../ssr", version = "^0.3.0", optional = true }
|
||||||
|
|
||||||
@@ -33,8 +35,12 @@ tokio = { version = "1.27.0", features = ["full"], optional = true }
|
|||||||
object-pool = "0.5.4"
|
object-pool = "0.5.4"
|
||||||
anymap = "0.12.1"
|
anymap = "0.12.1"
|
||||||
|
|
||||||
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
|
dioxus-hot-reload = { path = "../hot-reload" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["hot-reload"]
|
||||||
|
hot-reload = []
|
||||||
warp = ["dep:warp", "http-body", "ssr"]
|
warp = ["dep:warp", "http-body", "ssr"]
|
||||||
axum = ["dep:axum", "tower-http", "hyper", "ssr"]
|
axum = ["dep:axum", "tower-http", "hyper", "ssr"]
|
||||||
salvo = ["dep:salvo", "hyper", "ssr"]
|
salvo = ["dep:salvo", "hyper", "ssr"]
|
||||||
|
|||||||
@@ -6,40 +6,82 @@ use dioxus_ssr::Renderer;
|
|||||||
|
|
||||||
use crate::prelude::ServeConfig;
|
use crate::prelude::ServeConfig;
|
||||||
|
|
||||||
fn dioxus_ssr_html<P: 'static + Clone>(cfg: &ServeConfig<P>, renderer: &mut Renderer) -> String {
|
#[derive(Clone)]
|
||||||
let ServeConfig {
|
pub struct SSRState {
|
||||||
app,
|
// We keep a cache of renderers to avoid re-creating them on every request. They are boxed to make them very cheap to move
|
||||||
application_name,
|
renderers: Arc<object_pool::Pool<Renderer>>,
|
||||||
base_path,
|
#[cfg(all(debug_assertions, feature = "hot-reload"))]
|
||||||
head,
|
// The cache of all templates that have been modified since the last time we checked
|
||||||
props,
|
templates: Arc<std::sync::RwLock<std::collections::HashSet<dioxus_core::Template<'static>>>>,
|
||||||
..
|
}
|
||||||
} = cfg;
|
|
||||||
|
|
||||||
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
|
impl Default for SSRState {
|
||||||
let _ = vdom.rebuild();
|
fn default() -> Self {
|
||||||
|
#[cfg(all(debug_assertions, feature = "hot-reload"))]
|
||||||
|
let templates = {
|
||||||
|
let templates = Arc::new(std::sync::RwLock::new(std::collections::HashSet::new()));
|
||||||
|
dioxus_hot_reload::connect({
|
||||||
|
let templates = templates.clone();
|
||||||
|
move |msg| match msg {
|
||||||
|
dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => {
|
||||||
|
if let Ok(mut templates) = templates.write() {
|
||||||
|
templates.insert(template);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dioxus_hot_reload::HotReloadMsg::Shutdown => {
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
templates
|
||||||
|
};
|
||||||
|
|
||||||
let mut html = String::new();
|
Self {
|
||||||
|
renderers: Arc::new(object_pool::Pool::new(10, pre_renderer)),
|
||||||
|
#[cfg(all(debug_assertions, feature = "hot-reload"))]
|
||||||
|
templates,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let result = write!(
|
impl SSRState {
|
||||||
&mut html,
|
pub fn render<P: 'static + Clone>(&self, cfg: &ServeConfig<P>) -> String {
|
||||||
r#"
|
let ServeConfig {
|
||||||
|
app,
|
||||||
|
application_name,
|
||||||
|
base_path,
|
||||||
|
head,
|
||||||
|
props,
|
||||||
|
..
|
||||||
|
} = cfg;
|
||||||
|
|
||||||
|
let mut vdom = VirtualDom::new_with_props(*app, props.clone());
|
||||||
|
|
||||||
|
let _ = vdom.rebuild();
|
||||||
|
|
||||||
|
let mut renderer = self.renderers.pull(pre_renderer);
|
||||||
|
|
||||||
|
let mut html = String::new();
|
||||||
|
|
||||||
|
let result = write!(
|
||||||
|
&mut html,
|
||||||
|
r#"
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>{head}
|
<head>{head}
|
||||||
</head><body>
|
</head><body>
|
||||||
<div id="main">"#
|
<div id="main">"#
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
eprintln!("Failed to write to html: {}", err);
|
eprintln!("Failed to write to html: {}", err);
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = renderer.render_to(&mut html, &vdom);
|
let _ = renderer.render_to(&mut html, &vdom);
|
||||||
|
|
||||||
if let Err(err) = write!(
|
if let Err(err) = write!(
|
||||||
&mut html,
|
&mut html,
|
||||||
r#"</div>
|
r#"</div>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import init from "/{base_path}/assets/dioxus/{application_name}.js";
|
import init from "/{base_path}/assets/dioxus/{application_name}.js";
|
||||||
init("/{base_path}/assets/dioxus/{application_name}_bg.wasm").then(wasm => {{
|
init("/{base_path}/assets/dioxus/{application_name}_bg.wasm").then(wasm => {{
|
||||||
@@ -50,31 +92,11 @@ fn dioxus_ssr_html<P: 'static + Clone>(cfg: &ServeConfig<P>, renderer: &mut Rend
|
|||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>"#
|
</html>"#
|
||||||
) {
|
) {
|
||||||
eprintln!("Failed to write to html: {}", err);
|
eprintln!("Failed to write to html: {}", err);
|
||||||
}
|
|
||||||
|
|
||||||
html
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct SSRState {
|
|
||||||
// We keep a cache of renderers to avoid re-creating them on every request. They are boxed to make them very cheap to move
|
|
||||||
renderers: Arc<object_pool::Pool<Renderer>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for SSRState {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
renderers: Arc::new(object_pool::Pool::new(10, pre_renderer)),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SSRState {
|
html
|
||||||
pub fn render<P: 'static + Clone>(&self, cfg: &ServeConfig<P>) -> String {
|
|
||||||
let mut renderer = self.renderers.pull(pre_renderer);
|
|
||||||
dioxus_ssr_html(cfg, &mut renderer)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user