static_generation.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #![allow(non_snake_case)]
  2. use std::time::Duration;
  3. use dioxus::prelude::*;
  4. use dioxus_router::prelude::*;
  5. use dioxus_ssr::incremental::{DefaultRenderer, IncrementalRendererConfig};
  6. #[tokio::main]
  7. async fn main() {
  8. let mut renderer = IncrementalRendererConfig::new()
  9. .static_dir("./static")
  10. .invalidate_after(Duration::from_secs(10))
  11. .build();
  12. println!(
  13. "SITE MAP:\n{}",
  14. Route::SITE_MAP
  15. .iter()
  16. .flat_map(|route| route.flatten().into_iter())
  17. .map(|route| {
  18. route
  19. .iter()
  20. .map(|segment| segment.to_string())
  21. .collect::<Vec<_>>()
  22. .join("")
  23. })
  24. .collect::<Vec<_>>()
  25. .join("\n")
  26. );
  27. // This function is available if you enable the ssr feature
  28. // on the dioxus_router crate.
  29. pre_cache_static_routes::<Route, _>(
  30. &mut renderer,
  31. &DefaultRenderer {
  32. before_body: r#"<!DOCTYPE html>
  33. <html lang="en">
  34. <head>
  35. <meta charset="UTF-8">
  36. <meta name="viewport" content="width=device-width,
  37. initial-scale=1.0">
  38. <title>Dioxus Application</title>
  39. </head>
  40. <body>"#
  41. .to_string(),
  42. after_body: r#"</body>
  43. </html>"#
  44. .to_string(),
  45. },
  46. )
  47. .await
  48. .unwrap();
  49. }
  50. #[inline_props]
  51. fn Blog(cx: Scope) -> Element {
  52. render! {
  53. div {
  54. "Blog"
  55. }
  56. }
  57. }
  58. #[inline_props]
  59. fn Post(cx: Scope, id: usize) -> Element {
  60. render! {
  61. div {
  62. "PostId: {id}"
  63. }
  64. }
  65. }
  66. #[inline_props]
  67. fn PostHome(cx: Scope) -> Element {
  68. render! {
  69. div {
  70. "Post"
  71. }
  72. }
  73. }
  74. #[inline_props]
  75. fn Home(cx: Scope) -> Element {
  76. render! {
  77. div {
  78. "Home"
  79. }
  80. }
  81. }
  82. #[rustfmt::skip]
  83. #[derive(Clone, Debug, PartialEq, Routable)]
  84. enum Route {
  85. #[nest("/blog")]
  86. #[route("/")]
  87. Blog {},
  88. #[route("/post/index")]
  89. PostHome {},
  90. #[route("/post/:id")]
  91. Post {
  92. id: usize,
  93. },
  94. #[end_nest]
  95. #[route("/")]
  96. Home {},
  97. }