Skip to content

fix(sse): emit empty id field to reset Last-Event-ID - #5138

Merged
yusukebe merged 1 commit into
honojs:mainfrom
lasder-ca:fix/sse-empty-id
Jul 21, 2026
Merged

fix(sse): emit empty id field to reset Last-Event-ID#5138
yusukebe merged 1 commit into
honojs:mainfrom
lasder-ca:fix/sse-empty-id

Conversation

@lasder-ca

@lasder-ca lasder-ca commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Emit an SSE id field when SSEMessage.id is an empty string.

Problem

SSEStreamingApi.writeSSE() currently uses a truthy check when serializing message.id:

message.id && \id: ${message.id}``

This omits the field when id is an empty string.

In Server-Sent Events, an empty id field resets the client's last event ID. After the reset, a subsequent reconnect should not send a Last-Event-ID header.

Hono currently cannot express this behavior through writeSSE().

Specification:
https://html.spec.whatwg.org/multipage/server-sent-events.html#the-last-event-id-header

Change

Use an explicit undefined check:

message.id !== undefined && \id: ${message.id}``

This preserves the existing behavior when id is omitted while allowing an empty string to be serialized as an empty id field.

Test

Added a regression test using:

writeSSE({ data: 'reset', id: '' })

The test verifies the exact output:

data: reset
id:

Local result:

src/helper/streaming/sse.test.tsx
19 tests passed

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.04%. Comparing base (cadff88) to head (58158cf).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5138   +/-   ##
=======================================
  Coverage   79.04%   79.04%           
=======================================
  Files         154      154           
  Lines       10779    10779           
  Branches     2256     2256           
=======================================
  Hits         8520     8520           
  Misses       2259     2259           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
yusukebe merged commit 44f8843 into honojs:main Jul 21, 2026
20 checks passed
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