Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/fullstack-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) type ContextProviders = Arc<Vec<Box<dyn Fn() -> Box<dyn Any> + Send +
#[derive(Clone, Default)]
pub struct ServeConfig {
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) incremental: Option<IncrementalRendererConfig>,
pub(crate) context_providers: Vec<Arc<dyn Fn() -> Box<dyn Any> + Send + Sync + 'static>>,
Expand Down Expand Up @@ -46,7 +46,7 @@ impl ServeConfig {
Self {
root_id: None,
incremental: None,
index: IndexHtml::default(),
index: None,
index_html_override: None,
context_providers: Default::default(),
streaming_mode: StreamingMode::default(),
Expand Down Expand Up @@ -313,4 +313,32 @@ impl ServeConfig {
self.streaming_mode = StreamingMode::OutOfOrder;
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")
}
}
4 changes: 2 additions & 2 deletions packages/fullstack-server/src/index_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ impl IndexHtml {
head_after_title = new_head_after_title.to_string();
}

Ok(IndexHtml {
Ok(dbg!(IndexHtml {
head_before_title,
head_after_title,
title,
close_head,
post_main: post_main.to_string(),
after_closing_body_tag: "</body>".to_string() + after_closing_body_tag,
})
}))
}
}

Expand Down
17 changes: 0 additions & 17 deletions packages/fullstack-server/src/launch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! A launch function that creates an axum router for the LaunchBuilder

use crate::IndexHtml;
use crate::{server::DioxusRouterExt, RenderHandleState, ServeConfig};
use anyhow::Context;
use axum::{
Expand Down Expand Up @@ -68,22 +67,6 @@ async fn serve_server(
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
// and we use the generated address the CLI gives us
let address = dioxus_cli_config::fullstack_address_or_localhost();
Expand Down
4 changes: 3 additions & 1 deletion packages/fullstack-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ impl RenderHandleState {
/// 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();

state.config.resolve_index();

let response = state
.renderers
.clone()
Expand Down
12 changes: 6 additions & 6 deletions packages/fullstack-server/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,13 @@ impl SsrRendererPool {
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 {
to.write_str(&title)?;
} 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 =
virtual_dom.in_scope(ScopeId::ROOT, try_consume_context::<Rc<ServerDocument>>);
Expand All @@ -708,7 +708,7 @@ impl SsrRendererPool {
cfg: &ServeConfig,
to: &mut R,
) -> Result<(), IncrementalRendererError> {
to.write_str(&cfg.index.close_head)?;
to.write_str(&cfg.index.as_ref().unwrap().close_head)?;

// // #[cfg(feature = "document")]
// {
Expand Down Expand Up @@ -749,7 +749,7 @@ impl SsrRendererPool {
)?;
}
write!(to, r#"</script>"#,)?;
to.write_str(&cfg.index.post_main)?;
to.write_str(&cfg.index.as_ref().unwrap().post_main)?;

Ok(())
}
Expand All @@ -759,7 +759,7 @@ impl SsrRendererPool {
cfg: &ServeConfig,
to: &mut R,
) -> Result<(), IncrementalRendererError> {
to.write_str(&cfg.index.after_closing_body_tag)?;
to.write_str(&cfg.index.as_ref().unwrap().after_closing_body_tag)?;

Ok(())
}
Expand Down
Loading