1
0

render.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. //! A shared pool of renderers for efficient server side rendering.
  2. use crate::document::ServerDocument;
  3. use crate::streaming::{Mount, StreamingRenderer};
  4. use dioxus_interpreter_js::INITIALIZE_STREAMING_JS;
  5. use dioxus_isrg::{CachedRender, RenderFreshness};
  6. use dioxus_lib::document::Document;
  7. use dioxus_ssr::Renderer;
  8. use futures_channel::mpsc::Sender;
  9. use futures_util::{Stream, StreamExt};
  10. use std::rc::Rc;
  11. use std::sync::Arc;
  12. use std::sync::RwLock;
  13. use std::{collections::HashMap, future::Future};
  14. use tokio::task::JoinHandle;
  15. use crate::prelude::*;
  16. use dioxus_lib::prelude::*;
  17. /// A suspense boundary that is pending with a placeholder in the client
  18. struct PendingSuspenseBoundary {
  19. mount: Mount,
  20. children: Vec<ScopeId>,
  21. }
  22. /// Spawn a task in the background. If wasm is enabled, this will use the single threaded tokio runtime
  23. fn spawn_platform<Fut>(f: impl FnOnce() -> Fut + Send + 'static) -> JoinHandle<Fut::Output>
  24. where
  25. Fut: Future + 'static,
  26. Fut::Output: Send + 'static,
  27. {
  28. #[cfg(not(target_arch = "wasm32"))]
  29. {
  30. use tokio_util::task::LocalPoolHandle;
  31. static TASK_POOL: std::sync::OnceLock<LocalPoolHandle> = std::sync::OnceLock::new();
  32. let pool = TASK_POOL.get_or_init(|| {
  33. let threads = std::thread::available_parallelism()
  34. .unwrap_or(std::num::NonZeroUsize::new(1).unwrap());
  35. LocalPoolHandle::new(threads.into())
  36. });
  37. pool.spawn_pinned(f)
  38. }
  39. #[cfg(target_arch = "wasm32")]
  40. {
  41. tokio::task::spawn_local(f())
  42. }
  43. }
  44. struct SsrRendererPool {
  45. renderers: RwLock<Vec<Renderer>>,
  46. incremental_cache: Option<RwLock<dioxus_isrg::IncrementalRenderer>>,
  47. }
  48. impl SsrRendererPool {
  49. fn new(
  50. initial_size: usize,
  51. incremental: Option<dioxus_isrg::IncrementalRendererConfig>,
  52. ) -> Self {
  53. let renderers = RwLock::new((0..initial_size).map(|_| pre_renderer()).collect());
  54. Self {
  55. renderers,
  56. incremental_cache: incremental.map(|cache| RwLock::new(cache.build())),
  57. }
  58. }
  59. /// Look for a cached route in the incremental cache and send it into the render channel if it exists
  60. fn check_cached_route(
  61. &self,
  62. route: &str,
  63. render_into: &mut Sender<Result<String, dioxus_isrg::IncrementalRendererError>>,
  64. ) -> Option<RenderFreshness> {
  65. if let Some(incremental) = &self.incremental_cache {
  66. if let Ok(mut incremental) = incremental.write() {
  67. match incremental.get(route) {
  68. Ok(Some(cached_render)) => {
  69. let CachedRender {
  70. freshness,
  71. response,
  72. ..
  73. } = cached_render;
  74. _ = render_into.start_send(String::from_utf8(response.to_vec()).map_err(
  75. |err| dioxus_isrg::IncrementalRendererError::Other(Box::new(err)),
  76. ));
  77. return Some(freshness);
  78. }
  79. Err(e) => {
  80. tracing::error!(
  81. "Failed to get route \"{route}\" from incremental cache: {e}"
  82. );
  83. }
  84. _ => {}
  85. }
  86. }
  87. }
  88. None
  89. }
  90. /// Render a virtual dom into a stream. This method will return immediately and continue streaming the result in the background
  91. /// The streaming is canceled when the stream the function returns is dropped
  92. async fn render_to(
  93. self: Arc<Self>,
  94. cfg: &ServeConfig,
  95. route: String,
  96. virtual_dom_factory: impl FnOnce() -> VirtualDom + Send + Sync + 'static,
  97. server_context: &DioxusServerContext,
  98. ) -> Result<
  99. (
  100. RenderFreshness,
  101. impl Stream<Item = Result<String, dioxus_isrg::IncrementalRendererError>>,
  102. ),
  103. dioxus_isrg::IncrementalRendererError,
  104. > {
  105. struct ReceiverWithDrop {
  106. receiver: futures_channel::mpsc::Receiver<
  107. Result<String, dioxus_isrg::IncrementalRendererError>,
  108. >,
  109. cancel_task: Option<tokio::task::JoinHandle<()>>,
  110. }
  111. impl Stream for ReceiverWithDrop {
  112. type Item = Result<String, dioxus_isrg::IncrementalRendererError>;
  113. fn poll_next(
  114. mut self: std::pin::Pin<&mut Self>,
  115. cx: &mut std::task::Context<'_>,
  116. ) -> std::task::Poll<Option<Self::Item>> {
  117. self.receiver.poll_next_unpin(cx)
  118. }
  119. }
  120. // When we drop the stream, we need to cancel the task that is feeding values to the stream
  121. impl Drop for ReceiverWithDrop {
  122. fn drop(&mut self) {
  123. if let Some(cancel_task) = self.cancel_task.take() {
  124. cancel_task.abort();
  125. }
  126. }
  127. }
  128. let (mut into, rx) = futures_channel::mpsc::channel::<
  129. Result<String, dioxus_isrg::IncrementalRendererError>,
  130. >(1000);
  131. // before we even spawn anything, we can check synchronously if we have the route cached
  132. if let Some(freshness) = self.check_cached_route(&route, &mut into) {
  133. return Ok((
  134. freshness,
  135. ReceiverWithDrop {
  136. receiver: rx,
  137. cancel_task: None,
  138. },
  139. ));
  140. }
  141. let wrapper = FullstackHTMLTemplate { cfg: cfg.clone() };
  142. let server_context = server_context.clone();
  143. let mut renderer = self
  144. .renderers
  145. .write()
  146. .unwrap()
  147. .pop()
  148. .unwrap_or_else(pre_renderer);
  149. let myself = self.clone();
  150. let join_handle = spawn_platform(move || async move {
  151. let mut virtual_dom = virtual_dom_factory();
  152. let document = std::rc::Rc::new(crate::document::server::ServerDocument::default());
  153. virtual_dom.provide_root_context(document.clone());
  154. virtual_dom.provide_root_context(Rc::new(
  155. dioxus_history::MemoryHistory::with_initial_path(&route),
  156. ) as Rc<dyn dioxus_history::History>);
  157. virtual_dom.provide_root_context(document.clone() as std::rc::Rc<dyn Document>);
  158. // poll the future, which may call server_context()
  159. tracing::info!("Rebuilding vdom");
  160. with_server_context(server_context.clone(), || virtual_dom.rebuild_in_place());
  161. let mut pre_body = String::new();
  162. if let Err(err) = wrapper.render_head(&mut pre_body, &virtual_dom) {
  163. _ = into.start_send(Err(err));
  164. return;
  165. }
  166. let stream = Arc::new(StreamingRenderer::new(pre_body, into));
  167. let scope_to_mount_mapping = Arc::new(RwLock::new(HashMap::new()));
  168. renderer.pre_render = true;
  169. {
  170. let scope_to_mount_mapping = scope_to_mount_mapping.clone();
  171. let stream = stream.clone();
  172. // We use a stack to keep track of what suspense boundaries we are nested in to add children to the correct boundary
  173. // The stack starts with the root scope because the root is a suspense boundary
  174. let pending_suspense_boundaries_stack = RwLock::new(vec![]);
  175. renderer.set_render_components(move |renderer, to, vdom, scope| {
  176. let is_suspense_boundary =
  177. SuspenseContext::downcast_suspense_boundary_from_scope(
  178. &vdom.runtime(),
  179. scope,
  180. )
  181. .filter(|s| s.has_suspended_tasks())
  182. .is_some();
  183. if is_suspense_boundary {
  184. let mount = stream.render_placeholder(
  185. |to| {
  186. {
  187. pending_suspense_boundaries_stack
  188. .write()
  189. .unwrap()
  190. .push(scope);
  191. }
  192. let out = renderer.render_scope(to, vdom, scope);
  193. {
  194. pending_suspense_boundaries_stack.write().unwrap().pop();
  195. }
  196. out
  197. },
  198. &mut *to,
  199. )?;
  200. // Add the suspense boundary to the list of pending suspense boundaries
  201. // We will replace the mount with the resolved contents later once the suspense boundary is resolved
  202. let mut scope_to_mount_mapping_write =
  203. scope_to_mount_mapping.write().unwrap();
  204. scope_to_mount_mapping_write.insert(
  205. scope,
  206. PendingSuspenseBoundary {
  207. mount,
  208. children: vec![],
  209. },
  210. );
  211. // Add the scope to the list of children of the parent suspense boundary
  212. let pending_suspense_boundaries_stack =
  213. pending_suspense_boundaries_stack.read().unwrap();
  214. // If there is a parent suspense boundary, add the scope to the list of children
  215. // This suspense boundary will start capturing errors when the parent is resolved
  216. if let Some(parent) = pending_suspense_boundaries_stack.last() {
  217. let parent = scope_to_mount_mapping_write.get_mut(parent).unwrap();
  218. parent.children.push(scope);
  219. }
  220. // Otherwise this is a root suspense boundary, so we need to start capturing errors immediately
  221. else {
  222. vdom.in_runtime(|| {
  223. start_capturing_errors(scope);
  224. });
  225. }
  226. } else {
  227. renderer.render_scope(to, vdom, scope)?
  228. }
  229. Ok(())
  230. });
  231. }
  232. macro_rules! throw_error {
  233. ($e:expr) => {
  234. stream.close_with_error($e);
  235. return;
  236. };
  237. }
  238. // Render the initial frame with loading placeholders
  239. let mut initial_frame = renderer.render(&virtual_dom);
  240. // Along with the initial frame, we render the html after the main element, but before the body tag closes. This should include the script that starts loading the wasm bundle.
  241. if let Err(err) = wrapper.render_after_main(&mut initial_frame, &virtual_dom) {
  242. throw_error!(err);
  243. }
  244. stream.render(initial_frame);
  245. // After the initial render, we need to resolve suspense
  246. while virtual_dom.suspended_tasks_remaining() {
  247. ProvideServerContext::new(
  248. virtual_dom.wait_for_suspense_work(),
  249. server_context.clone(),
  250. )
  251. .await;
  252. let resolved_suspense_nodes = ProvideServerContext::new(
  253. virtual_dom.render_suspense_immediate(),
  254. server_context.clone(),
  255. )
  256. .await;
  257. // Just rerender the resolved nodes
  258. for scope in resolved_suspense_nodes {
  259. let pending_suspense_boundary = {
  260. let mut lock = scope_to_mount_mapping.write().unwrap();
  261. lock.remove(&scope)
  262. };
  263. // If the suspense boundary was immediately removed, it may not have a mount. We can just skip resolving it
  264. if let Some(pending_suspense_boundary) = pending_suspense_boundary {
  265. let mut resolved_chunk = String::new();
  266. // After we replace the placeholder in the dom with javascript, we need to send down the resolved data so that the client can hydrate the node
  267. let render_suspense = |into: &mut String| {
  268. renderer.reset_hydration();
  269. renderer.render_scope(into, &virtual_dom, scope)
  270. };
  271. let resolved_data = serialize_server_data(&virtual_dom, scope);
  272. if let Err(err) = stream.replace_placeholder(
  273. pending_suspense_boundary.mount,
  274. render_suspense,
  275. resolved_data,
  276. &mut resolved_chunk,
  277. ) {
  278. throw_error!(dioxus_isrg::IncrementalRendererError::RenderError(err));
  279. }
  280. stream.render(resolved_chunk);
  281. // Freeze the suspense boundary to prevent future reruns of any child nodes of the suspense boundary
  282. if let Some(suspense) =
  283. SuspenseContext::downcast_suspense_boundary_from_scope(
  284. &virtual_dom.runtime(),
  285. scope,
  286. )
  287. {
  288. suspense.freeze();
  289. // Go to every child suspense boundary and add an error boundary. Since we cannot rerun any nodes above the child suspense boundary,
  290. // we need to capture the errors and send them to the client as it resolves
  291. virtual_dom.in_runtime(|| {
  292. for &suspense_scope in pending_suspense_boundary.children.iter() {
  293. start_capturing_errors(suspense_scope);
  294. }
  295. });
  296. }
  297. }
  298. }
  299. }
  300. // After suspense is done, we render the html after the body
  301. let mut post_streaming = String::new();
  302. if let Err(err) = wrapper.render_after_body(&mut post_streaming) {
  303. throw_error!(err);
  304. }
  305. // If incremental rendering is enabled, add the new render to the cache without the streaming bits
  306. if let Some(incremental) = &self.incremental_cache {
  307. let mut cached_render = String::new();
  308. if let Err(err) = wrapper.render_head(&mut cached_render, &virtual_dom) {
  309. throw_error!(err);
  310. }
  311. cached_render.push_str(&post_streaming);
  312. if let Ok(mut incremental) = incremental.write() {
  313. let _ = incremental.cache(route, cached_render);
  314. }
  315. }
  316. stream.render(post_streaming);
  317. renderer.reset_render_components();
  318. myself.renderers.write().unwrap().push(renderer);
  319. });
  320. Ok((
  321. RenderFreshness::now(None),
  322. ReceiverWithDrop {
  323. receiver: rx,
  324. cancel_task: Some(join_handle),
  325. },
  326. ))
  327. }
  328. }
  329. /// Start capturing errors at a suspense boundary. If the parent suspense boundary is frozen, we need to capture the errors in the suspense boundary
  330. /// and send them to the client to continue bubbling up
  331. fn start_capturing_errors(suspense_scope: ScopeId) {
  332. // Add an error boundary to the scope
  333. suspense_scope.in_runtime(provide_error_boundary);
  334. }
  335. fn serialize_server_data(virtual_dom: &VirtualDom, scope: ScopeId) -> String {
  336. // After we replace the placeholder in the dom with javascript, we need to send down the resolved data so that the client can hydrate the node
  337. // Extract any data we serialized for hydration (from server futures)
  338. let html_data =
  339. crate::html_storage::HTMLData::extract_from_suspense_boundary(virtual_dom, scope);
  340. // serialize the server state into a base64 string
  341. html_data.serialized()
  342. }
  343. /// State used in server side rendering. This utilizes a pool of [`dioxus_ssr::Renderer`]s to cache static templates between renders.
  344. #[derive(Clone)]
  345. pub struct SSRState {
  346. // We keep a pool of renderers to avoid re-creating them on every request. They are boxed to make them very cheap to move
  347. renderers: Arc<SsrRendererPool>,
  348. }
  349. impl SSRState {
  350. /// Create a new [`SSRState`].
  351. pub fn new(cfg: &ServeConfig) -> Self {
  352. Self {
  353. renderers: Arc::new(SsrRendererPool::new(4, cfg.incremental.clone())),
  354. }
  355. }
  356. /// Render the application to HTML.
  357. pub async fn render<'a>(
  358. &'a self,
  359. route: String,
  360. cfg: &'a ServeConfig,
  361. virtual_dom_factory: impl FnOnce() -> VirtualDom + Send + Sync + 'static,
  362. server_context: &'a DioxusServerContext,
  363. ) -> Result<
  364. (
  365. RenderFreshness,
  366. impl Stream<Item = Result<String, dioxus_isrg::IncrementalRendererError>>,
  367. ),
  368. dioxus_isrg::IncrementalRendererError,
  369. > {
  370. self.renderers
  371. .clone()
  372. .render_to(cfg, route, virtual_dom_factory, server_context)
  373. .await
  374. }
  375. }
  376. /// The template that wraps the body of the HTML for a fullstack page. This template contains the data needed to hydrate server functions that were run on the server.
  377. pub struct FullstackHTMLTemplate {
  378. cfg: ServeConfig,
  379. }
  380. impl FullstackHTMLTemplate {
  381. /// Create a new [`FullstackHTMLTemplate`].
  382. pub fn new(cfg: &ServeConfig) -> Self {
  383. Self { cfg: cfg.clone() }
  384. }
  385. }
  386. impl FullstackHTMLTemplate {
  387. /// Render any content before the head of the page.
  388. pub fn render_head<R: std::fmt::Write>(
  389. &self,
  390. to: &mut R,
  391. virtual_dom: &VirtualDom,
  392. ) -> Result<(), dioxus_isrg::IncrementalRendererError> {
  393. let ServeConfig { index, .. } = &self.cfg;
  394. let title = {
  395. let document: Option<std::rc::Rc<ServerDocument>> =
  396. virtual_dom.in_runtime(|| ScopeId::ROOT.consume_context());
  397. // Collect any head content from the document provider and inject that into the head
  398. document.and_then(|document| document.title())
  399. };
  400. to.write_str(&index.head_before_title)?;
  401. if let Some(title) = title {
  402. to.write_str(&title)?;
  403. } else {
  404. to.write_str(&index.title)?;
  405. }
  406. to.write_str(&index.head_after_title)?;
  407. let document: Option<std::rc::Rc<ServerDocument>> =
  408. virtual_dom.in_runtime(|| ScopeId::ROOT.consume_context());
  409. if let Some(document) = document {
  410. // Collect any head content from the document provider and inject that into the head
  411. document.render(to)?;
  412. // Enable a warning when inserting contents into the head during streaming
  413. document.start_streaming();
  414. }
  415. self.render_before_body(to)?;
  416. Ok(())
  417. }
  418. /// Render any content before the body of the page.
  419. fn render_before_body<R: std::fmt::Write>(
  420. &self,
  421. to: &mut R,
  422. ) -> Result<(), dioxus_isrg::IncrementalRendererError> {
  423. let ServeConfig { index, .. } = &self.cfg;
  424. to.write_str(&index.close_head)?;
  425. write!(to, "<script>{INITIALIZE_STREAMING_JS}</script>")?;
  426. Ok(())
  427. }
  428. /// Render all content after the main element of the page.
  429. pub fn render_after_main<R: std::fmt::Write>(
  430. &self,
  431. to: &mut R,
  432. virtual_dom: &VirtualDom,
  433. ) -> Result<(), dioxus_isrg::IncrementalRendererError> {
  434. let ServeConfig { index, .. } = &self.cfg;
  435. // Collect the initial server data from the root node. For most apps, no use_server_futures will be resolved initially, so this will be full on `None`s.
  436. // Sending down those Nones are still important to tell the client not to run the use_server_futures that are already running on the backend
  437. let resolved_data = serialize_server_data(virtual_dom, ScopeId::ROOT);
  438. write!(
  439. to,
  440. r#"<script>window.initial_dioxus_hydration_data="{resolved_data}";</script>"#,
  441. )?;
  442. to.write_str(&index.post_main)?;
  443. Ok(())
  444. }
  445. /// Render all content after the body of the page.
  446. pub fn render_after_body<R: std::fmt::Write>(
  447. &self,
  448. to: &mut R,
  449. ) -> Result<(), dioxus_isrg::IncrementalRendererError> {
  450. let ServeConfig { index, .. } = &self.cfg;
  451. to.write_str(&index.after_closing_body_tag)?;
  452. Ok(())
  453. }
  454. /// Wrap a body in the template
  455. pub fn wrap_body<R: std::fmt::Write>(
  456. &self,
  457. to: &mut R,
  458. virtual_dom: &VirtualDom,
  459. body: impl std::fmt::Display,
  460. ) -> Result<(), dioxus_isrg::IncrementalRendererError> {
  461. self.render_head(to, virtual_dom)?;
  462. write!(to, "{body}")?;
  463. self.render_after_main(to, virtual_dom)?;
  464. self.render_after_body(to)?;
  465. Ok(())
  466. }
  467. }
  468. fn pre_renderer() -> Renderer {
  469. let mut renderer = Renderer::default();
  470. renderer.pre_render = true;
  471. renderer
  472. }