resolve the index before rendering (#4806)
Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
1 parent
5018074a12
commit
19ada89cec
5 files changed
+41
-28
No files matched your search
@@ -14,7 +14,7 @@ pub(crate) type ContextProviders = Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send +
|
|||||||
#[derive(Clone, Default)]
|
#[derive(Clone, Default)]
|
||||||
pub struct ServeConfig {
|
pub struct ServeConfig {
|
||||||
pub(crate) root_id: Option<&'static str>,
|
pub(crate) root_id: Option<&'static str>,
|
||||||
pub(crate) index: IndexHtml,
|
pub(crate) index: Option<IndexHtml>,
|
||||||
pub(crate) index_html_override: Option<String>,
|
pub(crate) index_html_override: Option<String>,
|
||||||
pub(crate) incremental: Option<IncrementalRendererConfig>,
|
pub(crate) incremental: Option<IncrementalRendererConfig>,
|
||||||
pub(crate) context_providers: Vec<Arc<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>,
|
pub(crate) context_providers: Vec<Arc<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>,
|
||||||
@@ -46,7 +46,7 @@ impl ServeConfig {
|
|||||||
Self {
|
Self {
|
||||||
root_id: None,
|
root_id: None,
|
||||||
incremental: None,
|
incremental: None,
|
||||||
index: IndexHtml::default(),
|
index: None,
|
||||||
index_html_override: None,
|
index_html_override: None,
|
||||||
context_providers: Default::default(),
|
context_providers: Default::default(),
|
||||||
streaming_mode: StreamingMode::default(),
|
streaming_mode: StreamingMode::default(),
|
||||||
@@ -313,4 +313,32 @@ impl ServeConfig {
|
|||||||
self.streaming_mode = StreamingMode::OutOfOrder;
|
self.streaming_mode = StreamingMode::OutOfOrder;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn resolve_index(&mut self) -> &IndexHtml {
|
||||||
|
// Create the IndexHtml object if the user has specified an override
|
||||||
|
if let Some(raw_index_html) = self.index_html_override.as_ref() {
|
||||||
|
self.index = Some(
|
||||||
|
IndexHtml::new(raw_index_html, self.root_id.unwrap_or("main"))
|
||||||
|
.expect("Failed to parse custom index.html"),
|
||||||
|
);
|
||||||
|
} else if !cfg!(target_arch = "wasm32") {
|
||||||
|
let public_path = crate::public_path();
|
||||||
|
let index_html_path = public_path.join("index.html");
|
||||||
|
|
||||||
|
if index_html_path.exists() {
|
||||||
|
let index_html = std::fs::read_to_string(index_html_path)
|
||||||
|
.expect("Failed to read index.html from public directory");
|
||||||
|
self.index = Some(
|
||||||
|
IndexHtml::new(&index_html, self.root_id.unwrap_or("main"))
|
||||||
|
.expect("Failed to parse index.html from public directory"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if self.index.is_none() {
|
||||||
|
self.index = Some(IndexHtml::default());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.index
|
||||||
|
.as_ref()
|
||||||
|
.expect("IndexHtml is always set after it is resolved")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -57,14 +57,14 @@ impl IndexHtml {
|
|||||||
head_after_title = new_head_after_title.to_string();
|
head_after_title = new_head_after_title.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(IndexHtml {
|
Ok(dbg!(IndexHtml {
|
||||||
head_before_title,
|
head_before_title,
|
||||||
head_after_title,
|
head_after_title,
|
||||||
title,
|
title,
|
||||||
close_head,
|
close_head,
|
||||||
post_main: post_main.to_string(),
|
post_main: post_main.to_string(),
|
||||||
after_closing_body_tag: "</body>".to_string() + after_closing_body_tag,
|
after_closing_body_tag: "</body>".to_string() + after_closing_body_tag,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
//! A launch function that creates an axum router for the LaunchBuilder
|
//! A launch function that creates an axum router for the LaunchBuilder
|
||||||
|
|
||||||
use crate::IndexHtml;
|
|
||||||
use crate::{server::DioxusRouterExt, RenderHandleState, ServeConfig};
|
use crate::{server::DioxusRouterExt, RenderHandleState, ServeConfig};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use axum::{
|
use axum::{
|
||||||
@@ -68,22 +67,6 @@ async fn serve_server(
|
|||||||
cfg.context_providers.push(arced);
|
cfg.context_providers.push(arced);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the IndexHtml object if the user has specified an override
|
|
||||||
if let Some(raw_index_html) = cfg.index_html_override.as_ref() {
|
|
||||||
cfg.index = IndexHtml::new(raw_index_html, cfg.root_id.unwrap_or("main"))
|
|
||||||
.expect("Failed to parse custom index.html");
|
|
||||||
} else if !cfg!(target_arch = "wasm32") {
|
|
||||||
let public_path = crate::public_path();
|
|
||||||
let index_html_path = public_path.join("index.html");
|
|
||||||
|
|
||||||
if index_html_path.exists() {
|
|
||||||
let index_html = std::fs::read_to_string(index_html_path)
|
|
||||||
.expect("Failed to read index.html from public directory");
|
|
||||||
cfg.index = IndexHtml::new(&index_html, cfg.root_id.unwrap_or("main"))
|
|
||||||
.expect("Failed to parse index.html from public directory");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the address the server should run on. If the CLI is running, the CLI proxies fullstack into the main address
|
// Get the address the server should run on. If the CLI is running, the CLI proxies fullstack into the main address
|
||||||
// and we use the generated address the CLI gives us
|
// and we use the generated address the CLI gives us
|
||||||
let address = dioxus_cli_config::fullstack_address_or_localhost();
|
let address = dioxus_cli_config::fullstack_address_or_localhost();
|
||||||
|
|||||||
@@ -284,9 +284,11 @@ impl RenderHandleState {
|
|||||||
/// axum::serve(listener, router).await.unwrap();
|
/// axum::serve(listener, router).await.unwrap();
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn render_handler(State(state): State<Self>, request: Request<Body>) -> Response {
|
pub async fn render_handler(State(mut state): State<Self>, request: Request<Body>) -> Response {
|
||||||
let (parts, _) = request.into_parts();
|
let (parts, _) = request.into_parts();
|
||||||
|
|
||||||
|
state.config.resolve_index();
|
||||||
|
|
||||||
let response = state
|
let response = state
|
||||||
.renderers
|
.renderers
|
||||||
.clone()
|
.clone()
|
||||||
|
|||||||
@@ -680,13 +680,13 @@ impl SsrRendererPool {
|
|||||||
document.and_then(|document| document.title())
|
document.and_then(|document| document.title())
|
||||||
};
|
};
|
||||||
|
|
||||||
to.write_str(&cfg.index.head_before_title)?;
|
to.write_str(&cfg.index.as_ref().unwrap().head_before_title)?;
|
||||||
if let Some(title) = title {
|
if let Some(title) = title {
|
||||||
to.write_str(&title)?;
|
to.write_str(&title)?;
|
||||||
} else {
|
} else {
|
||||||
to.write_str(&cfg.index.title)?;
|
to.write_str(&cfg.index.as_ref().unwrap().title)?;
|
||||||
}
|
}
|
||||||
to.write_str(&cfg.index.head_after_title)?;
|
to.write_str(&cfg.index.as_ref().unwrap().head_after_title)?;
|
||||||
|
|
||||||
let document =
|
let document =
|
||||||
virtual_dom.in_scope(ScopeId::ROOT, try_consume_context::<Rc<ServerDocument>>);
|
virtual_dom.in_scope(ScopeId::ROOT, try_consume_context::<Rc<ServerDocument>>);
|
||||||
@@ -708,7 +708,7 @@ impl SsrRendererPool {
|
|||||||
cfg: &ServeConfig,
|
cfg: &ServeConfig,
|
||||||
to: &mut R,
|
to: &mut R,
|
||||||
) -> Result<(), IncrementalRendererError> {
|
) -> Result<(), IncrementalRendererError> {
|
||||||
to.write_str(&cfg.index.close_head)?;
|
to.write_str(&cfg.index.as_ref().unwrap().close_head)?;
|
||||||
|
|
||||||
// // #[cfg(feature = "document")]
|
// // #[cfg(feature = "document")]
|
||||||
// {
|
// {
|
||||||
@@ -749,7 +749,7 @@ impl SsrRendererPool {
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
write!(to, r#"</script>"#,)?;
|
write!(to, r#"</script>"#,)?;
|
||||||
to.write_str(&cfg.index.post_main)?;
|
to.write_str(&cfg.index.as_ref().unwrap().post_main)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -759,7 +759,7 @@ impl SsrRendererPool {
|
|||||||
cfg: &ServeConfig,
|
cfg: &ServeConfig,
|
||||||
to: &mut R,
|
to: &mut R,
|
||||||
) -> Result<(), IncrementalRendererError> {
|
) -> Result<(), IncrementalRendererError> {
|
||||||
to.write_str(&cfg.index.after_closing_body_tag)?;
|
to.write_str(&cfg.index.as_ref().unwrap().after_closing_body_tag)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user