main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_context(1234u32)
  11. .launch(app);
  12. }
  13. fn app() -> Element {
  14. let mut count = use_signal(|| 12345);
  15. let mut text = use_signal(|| "...".to_string());
  16. rsx! {
  17. document::Title { "hello axum! {count}" }
  18. h1 { "hello axum! {count}" }
  19. button { class: "increment-button", onclick: move |_| count += 1, "Increment" }
  20. button {
  21. class: "server-button",
  22. onclick: move |_| async move {
  23. if let Ok(data) = get_server_data().await {
  24. println!("Client received: {}", data);
  25. text.set(data.clone());
  26. post_server_data(data).await.unwrap();
  27. }
  28. },
  29. "Run a server function!"
  30. }
  31. "Server said: {text}"
  32. div {
  33. id: "errors",
  34. Errors {}
  35. }
  36. }
  37. }
  38. #[cfg(feature = "server")]
  39. async fn assert_server_context_provided() {
  40. let FromContext(i): FromContext<u32> = extract().await.unwrap();
  41. assert_eq!(i, 1234u32);
  42. }
  43. #[server(PostServerData)]
  44. async fn post_server_data(data: String) -> Result<(), ServerFnError> {
  45. assert_server_context_provided().await;
  46. println!("Server received: {}", data);
  47. Ok(())
  48. }
  49. #[server(GetServerData)]
  50. async fn get_server_data() -> Result<String, ServerFnError> {
  51. assert_server_context_provided().await;
  52. Ok("Hello from the server!".to_string())
  53. }
  54. #[server]
  55. async fn server_error() -> Result<String, ServerFnError> {
  56. assert_server_context_provided().await;
  57. tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
  58. Err(ServerFnError::new("the server threw an error!"))
  59. }
  60. #[component]
  61. fn Errors() -> Element {
  62. rsx! {
  63. // This is a tricky case for suspense https://github.com/DioxusLabs/dioxus/issues/2570
  64. // Root suspense boundary is already resolved when the inner suspense boundary throws an error.
  65. // We need to throw the error from the inner suspense boundary on the server to the hydrated
  66. // suspense boundary on the client
  67. ErrorBoundary {
  68. handle_error: |_| rsx! {
  69. "Hmm, something went wrong."
  70. },
  71. SuspenseBoundary {
  72. fallback: |_: SuspenseContext| rsx! {
  73. div {
  74. "Loading..."
  75. }
  76. },
  77. ThrowsError {}
  78. }
  79. }
  80. }
  81. }
  82. #[component]
  83. pub fn ThrowsError() -> Element {
  84. use_server_future(server_error)?
  85. .unwrap()
  86. .map_err(|err| RenderError::Aborted(CapturedError::from_display(err)))?;
  87. rsx! {
  88. "success"
  89. }
  90. }