Skip to content

Commit d77f588

Browse files
fsecada01claude
andcommitted
fix(docs): replace JS EventSource with HTMX SSE extension in Litestar guide
The framework uses HTMX for client-side interaction — the SSE section was incorrectly showing raw JavaScript. Replaced with HTMX sse-connect / sse-swap markup consistent with the zero-JS philosophy. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d8fcd7f commit d77f588

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

docs/site-pages/litestar-guide.html

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -787,28 +787,30 @@ <h3 id="sse-component">StreamingComponent</h3>
787787
)</code></pre>
788788
</div>
789789

790-
<h3 id="sse-client">Client-side EventSource</h3>
791-
<p>A minimal browser snippet to consume the SSE stream and swap partial HTML. HTMX SSE extension handles this automatically if you prefer a zero-JS approach.</p>
790+
<h3 id="sse-client">Client-side with HTMX SSE</h3>
791+
<p>The framework uses HTMX for all client-side interaction. HTMX's built-in <a href="https://htmx.org/extensions/sse/" target="_blank" rel="noopener">SSE extension</a> handles the stream connection and DOM swapping — no custom JavaScript required.</p>
792792

793793
<div class="code-block">
794794
<div class="code-block__header">
795-
<span class="code-block__filename">report.html (script block)</span>
796-
<span class="code-block__lang">JavaScript</span>
795+
<span class="code-block__filename">report.html</span>
796+
<span class="code-block__lang">HTML</span>
797797
</div>
798-
<pre><code><span class="kw">const</span> source = <span class="kw">new</span> <span class="cls">EventSource</span>(
799-
<span class="str">`/components/report/stream/?event=generate&amp;report_id=<span class="kw">${</span>reportId<span class="kw">}</span>`</span>
800-
);
801-
802-
source.<span class="fn">addEventListener</span>(<span class="str">"patch"</span>, (e) =&gt; {
803-
<span class="cm">// server sends rendered HTML partial on each yield</span>
804-
document.<span class="fn">getElementById</span>(<span class="str">"report-root"</span>).innerHTML = e.data;
805-
});
806-
807-
source.<span class="fn">addEventListener</span>(<span class="str">"done"</span>, () =&gt; source.<span class="fn">close</span>());</code></pre>
798+
<pre><code><span class="cm">&lt;!-- Load HTMX + SSE extension --&gt;</span>
799+
&lt;script src=<span class="str">"https://unpkg.com/htmx.org@1.9.10"</span>&gt;&lt;/script&gt;
800+
&lt;script src=<span class="str">"https://unpkg.com/htmx.org/dist/ext/sse.js"</span>&gt;&lt;/script&gt;
801+
802+
<span class="cm">&lt;!-- SSE-powered report component --&gt;</span>
803+
&lt;div hx-ext=<span class="str">"sse"</span>
804+
sse-connect=<span class="str">"/components/report/stream"</span>
805+
sse-swap=<span class="str">"message"</span>
806+
hx-swap=<span class="str">"innerHTML"</span>&gt;
807+
<span class="cm">&lt;!-- Each SSE frame replaces this content --&gt;</span>
808+
&lt;p&gt;Generating report&amp;hellip;&lt;/p&gt;
809+
&lt;/div&gt;</code></pre>
808810
</div>
809811

810-
<div class="callout callout--warn">
811-
<strong>Browser connection limit.</strong> Each <code>EventSource</code> holds one HTTP/1.1 connection. Modern browsers cap concurrent connections per origin at 6. Prefer HTTP/2 in production (Litestar + uvicorn with <code>--http h2</code>) to avoid this ceiling.
812+
<div class="callout callout--insight">
813+
<strong>Zero JavaScript.</strong> HTMX's SSE extension opens the connection, receives each <code>data:</code> frame from <code>StreamingComponent</code>, and swaps the rendered HTML into the target element automatically. This is consistent with the framework's server-driven, minimal-JS philosophy.
812814
</div>
813815

814816
<!-- WEBSOCKET -->
@@ -922,7 +924,7 @@ <h2 id="full-example" data-n="06">Full Example App</h2>
922924
<li><span class="step-text">Open <code>http://localhost:8000</code> — the index page renders all three components server-side.</span></li>
923925
<li><span class="step-text">Click Increment / Decrement on the counter — each click POSTs to <code>/components/counter/</code> and swaps the HTML fragment.</span></li>
924926
<li><span class="step-text">Submit text in the sentiment box — the async handler awaits the (mock) ML service and re-renders the score badge.</span></li>
925-
<li><span class="step-text">Click Generate Report — an <code>EventSource</code> opens to <code>/components/report/stream/</code> and each section appears as it completes.</span></li>
927+
<li><span class="step-text">Click Generate Report — HTMX's SSE extension connects to <code>/components/report/stream/</code> and each section appears as it completes.</span></li>
926928
</ol>
927929

928930
<!-- TESTING -->

0 commit comments

Comments
 (0)