cfg.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /// Configuration for the WebSys renderer for the Dioxus VirtualDOM.
  2. ///
  3. /// This struct helps configure the specifics of hydration and render destination for WebSys.
  4. ///
  5. /// # Example
  6. ///
  7. /// ```rust, ignore
  8. /// dioxus::web::launch(App, |cfg| cfg.hydrate(true).root_name("myroot"))
  9. /// ```
  10. pub struct WebConfig {
  11. pub(crate) hydrate: bool,
  12. pub(crate) rootname: String,
  13. pub(crate) cached_strings: Vec<String>,
  14. }
  15. impl Default for WebConfig {
  16. fn default() -> Self {
  17. Self {
  18. hydrate: false,
  19. rootname: "main".to_string(),
  20. cached_strings: Vec::new(),
  21. }
  22. }
  23. }
  24. impl WebConfig {
  25. /// Enable SSR hydration
  26. ///
  27. /// This enables Dioxus to pick up work from a pre-renderd HTML file. Hydration will completely skip over any async
  28. /// work and suspended nodes.
  29. ///
  30. /// Dioxus will load up all the elements with the `dio_el` data attribute into memory when the page is loaded.
  31. pub fn hydrate(&mut self, f: bool) -> &mut Self {
  32. self.hydrate = f;
  33. self
  34. }
  35. /// Set the name of the element that Dioxus will use as the root.
  36. ///
  37. /// This is akint to calling React.render() on the element with the specified name.
  38. pub fn rootname(&mut self, name: impl Into<String>) -> &mut Self {
  39. self.rootname = name.into();
  40. self
  41. }
  42. /// Set the name of the element that Dioxus will use as the root.
  43. ///
  44. /// This is akint to calling React.render() on the element with the specified name.
  45. pub fn with_string_cache(&mut self, cache: Vec<String>) -> &mut Self {
  46. self.cached_strings = cache;
  47. self
  48. }
  49. }