Can streaming server functions work with Cloudflare tunnels? #3239
-
Hi guys! Describe the bug Leptos Dependencies Its actix-files = { version = "0.6", optional = true }
actix-web = { version = "4", optional = true, features = ["macros"] }
console_error_panic_hook = "0.1"
http = { version = "1.0.0", optional = true }
leptos = { version = "0.6" }
leptos_meta = { version = "0.6" }
leptos_actix = { version = "0.6", optional = true }
leptos_router = { version = "0.6" }
wasm-bindgen = "=0.2.95"
futures-util = "0.3.31"
tokio = "1.41.1" The extra dependencies, tokio and futures-util, are to access async functionality such as To Reproduce
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use server_fn::codec::{JsonStream, StreamingJson};
#[component]
pub fn App() -> impl IntoView {
provide_meta_context();
view! {
<Stylesheet id="leptos" href="/pkg/cloudflare-tunnel-streaming-bug.css"/>
<Title text="Welcome to Leptos"/>
<Router>
<main>
<Routes>
<Route path="" view=HomePage/>
<Route path="/*any" view=NotFound/>
</Routes>
</main>
</Router>
}
}
#[server(StreamCounter, output = StreamingJson)]
async fn stream_counter() -> Result<JsonStream<usize>, ServerFnError> {
use std::time::Duration;
let counter =
futures_util::
stream::
FuturesUnordered::
from_iter(
(0usize..=30).map(
|elapsed_seconds| async move {
tokio::
time::
sleep(
Duration::from_secs(elapsed_seconds as u64)
).await;
Ok(elapsed_seconds)
}
)
);
Ok(JsonStream::new(counter))
}
#[component]
fn HomePage() -> impl IntoView {
let (count, set_count) = create_signal(0);
spawn_local(
async move {
use futures_util::stream::StreamExt;
let mut counter = stream_counter()
.await
.expect("Failed to stream counter to client!")
.into_inner();
while let Some(Ok(streamed_count)) = counter.next().await {
set_count.set(streamed_count);
}
}
);
view! {
<h1>{count}</h1>
}
}
#[component]
fn NotFound() -> impl IntoView {
#[cfg(feature = "ssr")]
{
let resp = expect_context::<leptos_actix::ResponseOptions>();
resp.set_status(actix_web::http::StatusCode::NOT_FOUND);
}
view! {
<h1>"Not Found"</h1>
}
}
Expected behavior Actual behavior Additional context I suspect that cloudflare doesn't forward HTTP requests until they're complete, and because technically, the stream is still in progress, the page doesn't work. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Seems like there's no consistent Leptos based workaround for this and it really needs to be resolved on Cloudflare's end. |
Beta Was this translation helpful? Give feedback.
Seems like there's no consistent Leptos based workaround for this and it really needs to be resolved on Cloudflare's end.
cloudflare/cloudflared#199