1use actix_web::HttpResponse;
2use yew::html::BaseComponent;
3use yew::ServerRenderer;
4
5pub async fn render<V, VM, E>(args: VM) -> Result<HttpResponse, E>
7where
8 V: BaseComponent,
9 V: BaseComponent<Properties = VM>,
10 VM: Send + 'static,
11{
12 let renderer = ServerRenderer::<V>::with_props(|| args);
13 let html = renderer.render().await;
14 let html = format!("<!DOCTYPE html>\n{html}");
16 Ok(HttpResponse::Ok()
17 .content_type("text/html; charset=utf-8")
18 .body(html))
19}
20
21pub async fn render_turbo_stream<V, VM, E>(args: VM) -> Result<HttpResponse, E>
23where
24 V: BaseComponent,
25 V: BaseComponent<Properties = VM>,
26 VM: Send + 'static,
27{
28 let renderer = ServerRenderer::<V>::with_props(|| args);
29 let html = renderer.render().await;
30 Ok(HttpResponse::Ok()
31 .content_type("text/vnd.turbo-stream.html")
32 .body(html))
33}
34
35pub fn redirect<E>(path: impl Into<String>) -> Result<HttpResponse, E> {
38 let path: String = path.into();
39 Ok(HttpResponse::SeeOther()
40 .insert_header(("Location", path))
41 .finish())
42}