follow up to serveconfig (#4820)
* remove latent dbg! * cleanup serveconfig
This commit is contained in:
1 parent
7408a33be7
commit
306639ee9f
5 files changed
+120
-119
No files matched your search
@@ -1,11 +1,34 @@
|
||||
use anyhow::Context;
|
||||
use std::path::Path;
|
||||
|
||||
/// An `IndexHtml` represents the contents of an `index.html` file used to serve a web application.
|
||||
///
|
||||
/// This defines the static portion of your web application, typically generated by a tool like `dx`
|
||||
/// in conjunction with wasm-bindgen.
|
||||
///
|
||||
/// This structure expects a well-formed HTML document split into several sections:
|
||||
/// - `head_before_title`: The portion of the `<head>` section before the `<title
|
||||
/// - `title`: The `<title>` tag and its contents.
|
||||
/// - `head_after_title`: The portion of the `<head>` section after the `<title>` tag.
|
||||
/// - `close_head`: The closing `</head>` tag and any content following it.
|
||||
/// - `post_main`: The content following the main application container (e.g., `<div id="main">`).
|
||||
/// - `after_closing_body_tag`: The content following the closing `</body>` tag.
|
||||
///
|
||||
/// These fields are not explicitly exposed as part of the API, but are critical for dioxus-fullstack
|
||||
/// to properly inject server-rendered content and client-side bootstrapping scripts into the HTML.
|
||||
///
|
||||
/// The simplest HTML document that satisfies this structure is:
|
||||
///
|
||||
/// ```html
|
||||
/// <html>
|
||||
/// <head> </head>
|
||||
/// <body>
|
||||
/// <div id="main"></div>
|
||||
/// </body>
|
||||
/// </html>
|
||||
/// ```
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct IndexHtml {
|
||||
pub struct IndexHtml {
|
||||
pub(crate) head_before_title: String,
|
||||
pub(crate) head_after_title: String,
|
||||
pub(crate) title: String,
|
||||
@@ -20,7 +43,7 @@ impl IndexHtml {
|
||||
/// This function will parse the `index.html` and split it into sections for easier manipulation.
|
||||
/// The `root_id` parameter specifies the id of the main application container (e.g., "main")
|
||||
/// which your app will render into.
|
||||
pub(crate) fn new(contents: &str, root_id: &'static str) -> Result<IndexHtml, anyhow::Error> {
|
||||
pub fn new(contents: &str, root_id: &str) -> Result<IndexHtml, anyhow::Error> {
|
||||
let (pre_main, post_main) = contents.split_once(&format!("id=\"{root_id}\""))
|
||||
.with_context(|| format!("Failed to find id=\"{root_id}\" in index.html. The id is used to inject the application into the page."))?;
|
||||
|
||||
@@ -57,19 +80,29 @@ impl IndexHtml {
|
||||
head_after_title = new_head_after_title.to_string();
|
||||
}
|
||||
|
||||
Ok(dbg!(IndexHtml {
|
||||
Ok(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,
|
||||
}))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for IndexHtml {
|
||||
fn default() -> Self {
|
||||
/// Load an `IndexHtml` from a file at the given path.
|
||||
///
|
||||
/// This function reads the contents of the file and parses it into an `IndexHtml`.
|
||||
/// The `id` parameter specifies the id of the main application container (e.g., "main").
|
||||
pub fn from_file(path: &Path, id: &str) -> Result<IndexHtml, anyhow::Error> {
|
||||
let contents = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("Failed to read index.html from {}", path.display()))?;
|
||||
IndexHtml::new(&contents, id)
|
||||
}
|
||||
|
||||
/// Create a default `IndexHtml` suitable for server-side rendering (SSR) only applications.
|
||||
/// This will not include any customizations to the HTML nor any JavaScript to bootstrap a client-side app.
|
||||
pub fn ssr_only() -> Self {
|
||||
const DEFAULT: &str = r#"<!DOCTYPE html>
|
||||
<html>
|
||||
<head> </head>
|
||||
|
||||
Reference in new issue
Block a user