Skip to content

fix: handle newlines in SSE events to prevent content truncation #21#22

Open
alekseysotnikov wants to merge 1 commit intodynamic-alpha:mainfrom
alekseysotnikov:#21-SSE-Event-Truncation-with-Newlines-in-HTML
Open

fix: handle newlines in SSE events to prevent content truncation #21#22
alekseysotnikov wants to merge 1 commit intodynamic-alpha:mainfrom
alekseysotnikov:#21-SSE-Event-Truncation-with-Newlines-in-HTML

Conversation

@alekseysotnikov
Copy link
Contributor

@alekseysotnikov alekseysotnikov commented Mar 12, 2026

When HTML content contains newline characters (\n), the SSE event formatting was not escaping them properly. This caused SSE events to be prematurely terminated, resulting in truncated content on the client side during live-reload updates.

Root cause: format-datastar-fragment was outputting HTML directly in a single data: elements line without handling newlines. Any \n in the HTML would prematurely end the SSE event.

Fix: Split HTML by \n and emit multiple data: elements lines, which Datastar concatenates. This follows Datastar's documented multi-line SSE format and prevents \n from breaking the event stream. #21

Copy link
Contributor

@rschmukler rschmukler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will squash and merge asap.

When HTML content contains newline characters (\n), the SSE event formatting
was not escaping them properly. This caused SSE events to be prematurely
terminated, resulting in truncated content on the client side during
live-reload updates.

Root cause: format-datastar-fragment was outputting HTML directly in a
single 'data: elements' line without handling newlines. Any \n\n in
the HTML would prematurely end the SSE event according to the SSE spec.

Fix: Split HTML by \n and emit multiple 'data: elements' lines, which
Datastar concatenates. This follows Datastar's documented multi-line
SSE format and prevents \n\n from breaking the event stream.

Tests added:
- Unit tests for textarea and pre elements with newline content
- E2E test for content with newlines during route redefinition
@alekseysotnikov alekseysotnikov force-pushed the #21-SSE-Event-Truncation-with-Newlines-in-HTML branch from 87edb32 to d565e35 Compare March 13, 2026 03:27
@alekseysotnikov
Copy link
Contributor Author

Thank you for instant feedback! I have squashed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants