main.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // This test is used by playwright configured in the root of the repo
  2. // Tests:
  3. // - Server functions
  4. // - SSR
  5. // - Hydration
  6. #![allow(non_snake_case)]
  7. use dioxus::{prelude::*, CapturedError};
  8. fn main() {
  9. dioxus::LaunchBuilder::new()
  10. .with_cfg(server_only! {
  11. dioxus::fullstack::ServeConfig::builder().enable_out_of_order_streaming()
  12. })
  13. .with_context(1234u32)
  14. .launch(app);
  15. }
  16. fn app() -> Element {
  17. let mut count = use_signal(|| 12345);
  18. let mut text = use_signal(|| "...".to_string());
  19. rsx! {
  20. document::Title { "hello axum! {count}" }
  21. h1 { "hello axum! {count}" }
  22. button { class: "increment-button", onclick: move |_| count += 1, "Increment" }
  23. button {
  24. class: "server-button",
  25. onclick: move |_| async move {
  26. if let Ok(data) = get_server_data().await {
  27. println!("Client received: {}", data);
  28. text.set(data.clone());
  29. post_server_data(data).await.unwrap();
  30. }
  31. },
  32. "Run a server function!"
  33. }
  34. "Server said: {text}"
  35. div {
  36. id: "errors",
  37. Errors {}
  38. }
  39. OnMounted {}
  40. DefaultServerFnCodec {}
  41. DocumentElements {}
  42. }
  43. }
  44. #[component]
  45. fn OnMounted() -> Element {
  46. let mut mounted_triggered_count = use_signal(|| 0);
  47. rsx! {
  48. div {
  49. class: "onmounted-div",
  50. onmounted: move |_| {
  51. mounted_triggered_count += 1;
  52. },
  53. "onmounted was called {mounted_triggered_count} times"
  54. }
  55. }
  56. }
  57. #[component]
  58. fn DefaultServerFnCodec() -> Element {
  59. let resource = use_server_future(|| get_server_data_empty_vec(Vec::new()))?;
  60. let empty_vec = resource.unwrap().unwrap();
  61. assert!(empty_vec.is_empty());
  62. rsx! {}
  63. }
  64. #[cfg(feature = "server")]
  65. async fn assert_server_context_provided() {
  66. let FromContext(i): FromContext<u32> = extract().await.unwrap();
  67. assert_eq!(i, 1234u32);
  68. }
  69. #[server(PostServerData)]
  70. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  71. assert_server_context_provided().await;
  72. println!("Server received: {}", data);
  73. Ok(())
  74. }
  75. #[server(GetServerData)]
  76. async fn get_server_data() -> Result<String, ServerFnError> {
  77. assert_server_context_provided().await;
  78. Ok("Hello from the server!".to_string())
  79. }
  80. // Make sure the default codec work with empty data structures
  81. // Regression test for https://github.com/DioxusLabs/dioxus/issues/2628
  82. #[server]
  83. async fn get_server_data_empty_vec(empty_vec: Vec<String>) -> Result<Vec<String>, ServerFnError> {
  84. assert_server_context_provided().await;
  85. assert!(empty_vec.is_empty());
  86. Ok(Vec::new())
  87. }
  88. #[server]
  89. async fn server_error() -> Result<String, ServerFnError> {
  90. assert_server_context_provided().await;
  91. tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
  92. Err(ServerFnError::new("the server threw an error!"))
  93. }
  94. #[component]
  95. fn Errors() -> Element {
  96. rsx! {
  97. // This is a tricky case for suspense https://github.com/DioxusLabs/dioxus/issues/2570
  98. // Root suspense boundary is already resolved when the inner suspense boundary throws an error.
  99. // We need to throw the error from the inner suspense boundary on the server to the hydrated
  100. // suspense boundary on the client
  101. ErrorBoundary {
  102. handle_error: |_| rsx! {
  103. "Hmm, something went wrong."
  104. },
  105. SuspenseBoundary {
  106. fallback: |_: SuspenseContext| rsx! {
  107. div {
  108. "Loading..."
  109. }
  110. },
  111. ThrowsError {}
  112. }
  113. }
  114. }
  115. }
  116. #[component]
  117. pub fn ThrowsError() -> Element {
  118. use_server_future(server_error)?
  119. .unwrap()
  120. .map_err(|err| RenderError::Aborted(CapturedError::from_display(err)))?;
  121. rsx! {
  122. "success"
  123. }
  124. }
  125. /// This component tests the document::* elements pre-rendered on the server
  126. #[component]
  127. fn DocumentElements() -> Element {
  128. rsx! {
  129. document::Meta { id: "meta-head", name: "testing", data: "dioxus-meta-element" }
  130. document::Link {
  131. id: "link-head",
  132. rel: "stylesheet",
  133. href: "https://fonts.googleapis.com/css?family=Roboto+Mono"
  134. }
  135. document::Stylesheet { id: "stylesheet-head", href: "https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic" }
  136. document::Script { id: "script-head", async: true, "console.log('hello world');" }
  137. document::Style { id: "style-head", "body {{ font-family: 'Roboto'; }}" }
  138. }
  139. }