2
0
Fork 0
mirror of https://github.com/DioxusLabs/dioxus synced 2024-12-23 02:53:12 +00:00
dioxus/packages/router/examples/static_generation.rs

101 lines
1.9 KiB
Rust
Raw Normal View History

2023-06-01 22:31:13 +00:00
#![allow(non_snake_case)]
use std::time::Duration;
2023-06-01 22:31:13 +00:00
use dioxus::prelude::*;
use dioxus_router::prelude::*;
2023-06-22 01:51:40 +00:00
2023-06-22 23:51:48 +00:00
use dioxus_ssr::incremental::{DefaultRenderer, IncrementalRendererConfig};
2023-06-01 22:31:13 +00:00
2023-06-24 20:44:22 +00:00
#[tokio::main]
async fn main() {
2023-06-22 01:36:32 +00:00
let mut renderer = IncrementalRendererConfig::new(DefaultRenderer {
before_body: r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Dioxus Application</title>
</head>
<body>"#
.to_string(),
after_body: r#"</body>
</html>"#
.to_string(),
})
.static_dir("./static")
.invalidate_after(Duration::from_secs(10))
2023-06-22 01:36:32 +00:00
.build();
2023-06-01 22:31:13 +00:00
2023-06-24 20:44:22 +00:00
pre_cache_static_routes::<Route, _>(&mut renderer)
.await
.unwrap();
2023-06-01 22:31:13 +00:00
for _ in 0..1_000_000 {
2023-06-22 01:36:32 +00:00
for id in 0..10 {
2023-06-24 20:44:22 +00:00
render_route(
&mut renderer,
Route::Post { id },
&mut tokio::io::sink(),
|_| {},
)
.await
.unwrap();
2023-06-01 22:31:13 +00:00
}
}
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn Blog(cx: Scope) -> Element {
2023-06-01 22:31:13 +00:00
render! {
2023-06-22 01:36:32 +00:00
div {
"Blog"
2023-06-01 22:31:13 +00:00
}
}
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn Post(cx: Scope, id: usize) -> Element {
2023-06-01 22:31:13 +00:00
render! {
div {
2023-06-22 01:36:32 +00:00
"PostId: {id}"
2023-06-01 22:31:13 +00:00
}
}
}
#[inline_props]
2023-06-22 01:36:32 +00:00
fn PostHome(cx: Scope) -> Element {
2023-06-01 22:31:13 +00:00
render! {
div {
"Post"
}
}
}
#[inline_props]
fn Home(cx: Scope) -> Element {
render! {
div {
"Home"
}
}
}
#[rustfmt::skip]
#[derive(Clone, Debug, PartialEq, Routable)]
enum Route {
#[nest("/blog")]
#[route("/")]
Blog {},
2023-06-22 01:36:32 +00:00
#[route("/post/index")]
PostHome {},
#[route("/post/:id")]
Post {
id: usize,
},
2023-06-01 22:31:13 +00:00
#[end_nest]
#[route("/")]
Home {},
}