Move the document trait into a separate crate (#3035)

* add a default head method through eval
* remove the old document trait
* implement document for each platform
* pull out document into a dedicated crate to cut down on shared dependencies
---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2024-10-14 10:33:37 -07:00
1 parent 2b219c826c
commit 519ec9d294
91 files changed
+1113 -1044

No files matched your search

+4 -9
View File
@@ -4,9 +4,8 @@
use std::cell::RefCell;
use dioxus_lib::{html::document::*, prelude::*};
use dioxus_lib::{document::*, prelude::*};
use dioxus_ssr::Renderer;
use generational_box::GenerationalBox;
use once_cell::sync::Lazy;
use parking_lot::RwLock;
@@ -71,8 +70,8 @@ impl ServerDocument {
}
impl Document for ServerDocument {
fn new_evaluator(&self, js: String) -> GenerationalBox<Box<dyn Evaluator>> {
NoOpDocument.new_evaluator(js)
fn eval(&self, js: String) -> Eval {
NoOpDocument.eval(js)
}
fn set_title(&self, title: String) {
@@ -151,7 +150,7 @@ impl Document for ServerDocument {
}
}
fn create_link(&self, props: head::LinkProps) {
fn create_link(&self, props: LinkProps) {
self.warn_if_streaming();
self.serialize_for_hydration();
self.0.borrow_mut().link.push(rsx! {
@@ -173,8 +172,4 @@ impl Document for ServerDocument {
}
})
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+7 -14
View File
@@ -1,7 +1,7 @@
#![allow(unused)]
//! On the client, we use the [`WebDocument`] implementation to render the head for any elements that were not rendered on the server.
use dioxus_lib::events::Document;
use dioxus_lib::document::*;
use dioxus_web::WebDocument;
fn head_element_written_on_server() -> bool {
@@ -15,11 +15,8 @@ fn head_element_written_on_server() -> bool {
pub struct FullstackWebDocument;
impl Document for FullstackWebDocument {
fn new_evaluator(
&self,
js: String,
) -> generational_box::GenerationalBox<Box<dyn dioxus_lib::prelude::document::Evaluator>> {
WebDocument.new_evaluator(js)
fn eval(&self, js: String) -> Eval {
WebDocument.eval(js)
}
fn set_title(&self, title: String) {
@@ -29,35 +26,31 @@ impl Document for FullstackWebDocument {
WebDocument.set_title(title);
}
fn create_meta(&self, props: dioxus_lib::prelude::MetaProps) {
fn create_meta(&self, props: MetaProps) {
if head_element_written_on_server() {
return;
}
WebDocument.create_meta(props);
}
fn create_script(&self, props: dioxus_lib::prelude::ScriptProps) {
fn create_script(&self, props: ScriptProps) {
if head_element_written_on_server() {
return;
}
WebDocument.create_script(props);
}
fn create_style(&self, props: dioxus_lib::prelude::StyleProps) {
fn create_style(&self, props: StyleProps) {
if head_element_written_on_server() {
return;
}
WebDocument.create_style(props);
}
fn create_link(&self, props: dioxus_lib::prelude::head::LinkProps) {
fn create_link(&self, props: LinkProps) {
if head_element_written_on_server() {
return;
}
WebDocument.create_link(props);
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
+5 -8
View File
@@ -1,7 +1,9 @@
//! A shared pool of renderers for efficient server side rendering.
use crate::document::ServerDocument;
use crate::streaming::{Mount, StreamingRenderer};
use dioxus_interpreter_js::INITIALIZE_STREAMING_JS;
use dioxus_isrg::{CachedRender, RenderFreshness};
use dioxus_lib::document::Document;
use dioxus_ssr::Renderer;
use futures_channel::mpsc::Sender;
use futures_util::{Stream, StreamExt};
@@ -165,6 +167,7 @@ impl SsrRendererPool {
let join_handle = spawn_platform(move || async move {
let mut virtual_dom = virtual_dom_factory();
let document = std::rc::Rc::new(crate::document::server::ServerDocument::default());
virtual_dom.provide_root_context(document.clone());
virtual_dom.provide_root_context(document.clone() as std::rc::Rc<dyn Document>);
// poll the future, which may call server_context()
@@ -431,11 +434,8 @@ impl FullstackHTMLTemplate {
let ServeConfig { index, .. } = &self.cfg;
let title = {
let document: Option<std::rc::Rc<dyn Document>> =
let document: Option<std::rc::Rc<ServerDocument>> =
virtual_dom.in_runtime(|| ScopeId::ROOT.consume_context());
let document: Option<&crate::document::server::ServerDocument> = document
.as_ref()
.and_then(|document| document.as_any().downcast_ref());
// Collect any head content from the document provider and inject that into the head
document.and_then(|document| document.title())
};
@@ -448,11 +448,8 @@ impl FullstackHTMLTemplate {
}
to.write_str(&index.head_after_title)?;
let document: Option<std::rc::Rc<dyn dioxus_lib::prelude::document::Document>> =
let document: Option<std::rc::Rc<ServerDocument>> =
virtual_dom.in_runtime(|| ScopeId::ROOT.consume_context());
let document: Option<&crate::document::server::ServerDocument> = document
.as_ref()
.and_then(|document| document.as_any().downcast_ref());
if let Some(document) = document {
// Collect any head content from the document provider and inject that into the head
document.render(to)?;