dioxus_fullstack_core/
document.rs

1//! On the client, we use the [`FullstackWebDocument`] implementation to render the head for any elements that were not rendered on the server.
2
3use dioxus_document::*;
4
5fn head_element_written_on_server() -> bool {
6    crate::transport::head_element_hydration_entry()
7        .get()
8        .ok()
9        .unwrap_or_default()
10}
11
12/// A document provider for fullstack web clients
13#[derive(Clone)]
14pub struct FullstackWebDocument<D> {
15    document: D,
16}
17
18impl<D> From<D> for FullstackWebDocument<D> {
19    fn from(document: D) -> Self {
20        Self { document }
21    }
22}
23
24impl<D: Document> Document for FullstackWebDocument<D> {
25    fn eval(&self, js: String) -> Eval {
26        self.document.eval(js)
27    }
28
29    /// Set the title of the document
30    fn set_title(&self, title: String) {
31        self.document.set_title(title);
32    }
33
34    /// Create a new meta tag in the head
35    fn create_meta(&self, props: MetaProps) {
36        self.document.create_meta(props);
37    }
38
39    /// Create a new script tag in the head
40    fn create_script(&self, props: ScriptProps) {
41        self.document.create_script(props);
42    }
43
44    /// Create a new style tag in the head
45    fn create_style(&self, props: StyleProps) {
46        self.document.create_style(props);
47    }
48
49    /// Create a new link tag in the head
50    fn create_link(&self, props: LinkProps) {
51        self.document.create_link(props);
52    }
53
54    fn create_head_component(&self) -> bool {
55        !head_element_written_on_server()
56    }
57}