finish server function docs

This commit is contained in:
Evan Almloff committed 2023-05-04 09:57:11 -05:00
1 parent 515aee6c3c
commit ab1de6e53f
4 files changed
+163 -11

No files matched your search

+18 -5
View File
@@ -35,11 +35,11 @@ fn main() {
// Render the application. This will serialize the root props (the intial count) into the HTML
.route(
"/",
get(move |Path(intial_count): Path<usize>, State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
get(move |State(ssr_state): State<SSRState>| async move { axum::body::Full::from(
ssr_state.render(
&ServeConfigBuilder::new(
app,
intial_count,
0,
)
.build(),
)
@@ -77,9 +77,10 @@ fn app(cx: Scope<usize>) -> Element {
button {
onclick: move |_| {
to_owned![count];
let sc = cx.sc();
async move {
// Call the server function just like a local async function
if let Ok(new_count) = double_server(*count.current()).await {
if let Ok(new_count) = double_server(sc, *count.current()).await {
count.set(new_count);
}
}
@@ -89,11 +90,23 @@ fn app(cx: Scope<usize>) -> Element {
})
}
#[server(DoubleServer)]
async fn double_server(number: usize) -> Result<usize, ServerFnError> {
// We use the "getcbor" encoding to make caching easier
#[server(DoubleServer, "", "getcbor")]
async fn double_server(cx: DioxusServerContext, number: usize) -> Result<usize, ServerFnError> {
// Perform some expensive computation or access a database on the server
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
let result = number * 2;
println!(
"User Agent {:?}",
cx.request_parts().headers.get("User-Agent")
);
// Set the cache control header to 1 hour on the post request
cx.responce_headers_mut()
.insert("Cache-Control", "max-age=3600".parse().unwrap());
println!("server calculated {result}");
Ok(result)
}