Skip to content

fix: preserve lastEventId when a later event omits the id field - #358

Merged
rexxars merged 4 commits into
EventSource:mainfrom
pacocartones:fix/lasteventid-persista-tras-omitir-id
Aug 20, 2026
Merged

fix: preserve lastEventId when a later event omits the id field#358
rexxars merged 4 commits into
EventSource:mainfrom
pacocartones:fix/lasteventid-persista-tras-omitir-id

Conversation

@pacocartones

Copy link
Copy Markdown
Contributor

What

EventSource#onEvent set each message event's lastEventId to event.id || '', i.e. the id of the current event. Whenever an event arrived without an id field, this blanked lastEventId instead of carrying the previously seen value.

Per the WHATWG spec (server-sent events, "dispatch the event"), a message event's lastEventId is initialized from the connection's last event ID buffer, which is only updated by an explicit id field and must survive events that omit it.

Fix

Emit the persisted #lastEventId buffer (coerced to '' when still unset) instead of the current event's id.

-    const lastEventId = event.id || ''
+    const lastEventId = this.#lastEventId ?? ''

The #lastEventId buffer is already updated correctly a few lines above (if (typeof event.id === 'string') this.#lastEventId = event.id), so this is a one-line behavioural fix plus a comment.

Test

Adds a /mixed-ids test endpoint that writes one event with id: 1 then one without, and a test that records every event (the two arrive back to back, so lastArg alone would already point at the second by the time the first is asserted). The second event must still carry lastEventId: '1'.

  • Red (before the fix): the second event arrives with lastEventId: ''.
  • Green (after): lastEventId: '1' for both.

test/client.test.ts node suite: 41 passed, 1 skipped (the pre-existing [NON-SPEC] case that needs a live cross-origin connection). Red re-verified against the unfixed source.

@Elonreevemusk090 Elonreevemusk090 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Change

Comment thread .changeset/lasteventid-persists-without-id.md Outdated
Comment thread src/EventSource.ts Outdated
Comment thread test/client.test.ts
Co-authored-by: Espen Hovlandsdal <espen@hovlandsdal.com>
@rexxars
rexxars merged commit de3c0ae into EventSource:main Aug 20, 2026
1 check passed
@rexxars

rexxars commented Aug 20, 2026

Copy link
Copy Markdown
Member

Thanks! I found a semi-related issue that I'm working on fixing, then I'll do a release.

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.

3 participants