My personal site cherry.computer
htmx tailwind axum askama

fix: pass query parameter to fetch scrobble if SSR timed out

cherry.computer f42751e3 0f0fe58c

verified
+15 -3
+2 -1
frontend/esbuild.js
··· 42 42 method: req.method, 43 43 headers: req.headers, 44 44 }; 45 + const url = new URL(`http://localhost${req.url}`); 45 46 const route = 46 - req.url === "/" || req.url === "/scrobbles" 47 + url.pathname === "/" || url.pathname === "/scrobbles" 47 48 ? { hostname: "127.0.0.1", port: 8080 } 48 49 : { hostname: host, port }; 49 50 const routedOptions = { ...options, ...route };
+12 -1
server/src/main.rs
··· 10 10 use askama::Template; 11 11 use async_stream::stream; 12 12 use axum::{ 13 + extract::Query, 13 14 http::{HeaderName, HeaderValue, StatusCode}, 14 15 response::{sse, Html, IntoResponse, Sse}, 15 16 routing::{get, get_service}, 16 17 Extension, Router, 17 18 }; 19 + use serde::Deserialize; 18 20 use tokio::time::{self, MissedTickBehavior}; 19 21 use tokio_stream::Stream; 20 22 use tower::ServiceBuilder; ··· 60 62 }) 61 63 } 62 64 65 + #[derive(Deserialize)] 66 + struct ScrobbleQuery { 67 + #[serde(default)] 68 + immediate: bool, 69 + } 70 + 63 71 async fn get_scrobble( 64 72 Extension(mut monitor): Extension<ScrobbleMonitor>, 73 + Query(ScrobbleQuery { immediate }): Query<ScrobbleQuery>, 65 74 ) -> Sse<impl Stream<Item = Result<sse::Event, Infallible>>> { 66 75 let stream = stream! { 67 76 let mut interval = time::interval(Duration::from_secs(30)); 68 77 interval.set_missed_tick_behavior(MissedTickBehavior::Skip); 69 - interval.tick().await; 78 + if !immediate { 79 + interval.tick().await; 80 + } 70 81 loop { 71 82 interval.tick().await; 72 83 let template = match monitor.get_scrobble().await {
+1 -1
server/templates/index.html
··· 89 89 <div 90 90 class="scrobble-bar" 91 91 hx-ext="sse" 92 - sse-connect="/scrobbles" 92 + sse-connect="/scrobbles{% if scrobble.is_none() %}?immediate=true{% endif %}" 93 93 sse-swap="scrobble" 94 94 > 95 95 {%- match scrobble %} {%- when Some with (ScrobblesTemplate {intro, now_playing, image, srcset}) %}